Created
February 12, 2017 19:21
-
-
Save evercode1/87dbf80af540e307df9f197013745379 to your computer and use it in GitHub Desktop.
chapter 15 ValidationServiceProvider.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Providers; | |
| use Illuminate\Support\ServiceProvider; | |
| use App\Subcategory; | |
| use App\Category; | |
| class ValidatorServiceProvider extends ServiceProvider | |
| { | |
| /** | |
| * Bootstrap the application services. | |
| * | |
| * @return void | |
| */ | |
| public function boot() | |
| { | |
| $this->app['validator']->extend('isValidCategory', function ($attribute, $value, $parameters) | |
| { | |
| $max = Category::count(); | |
| $verified = (1 <= $value) && ($value <= $max) ? true : false; | |
| return 'isValidCategory' == $verified; | |
| }, | |
| 'Sorry, category is not a valid category!'); | |
| $this->app['validator']->extend('belongsToCategory', function ($attribute, $value, $parameters) | |
| { | |
| $category_id = request()->input('category_id'); | |
| $verified = Subcategory::where('id', $value) | |
| ->where('category_id', $category_id) | |
| ->exists(); | |
| return 'belongsToCategory' == $verified; | |
| }, | |
| 'Sorry, subcategory does not belong to category!'); | |
| } | |
| /** | |
| * Register the application services. | |
| * | |
| * @return void | |
| */ | |
| public function register() | |
| { | |
| // | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment