Laravel 5 introducing with changed directory structure, psr-4 auto-loading standard by default usage, dependency injection support on route and controller methods and form validation with form requests.
Once we completed validate data in Laravel by override default data validation package and abstract out from data validation to entity by specific services to make the reusable code.
Basic recipe to create custom validation
1) Create own custom validation class
Now we have a new rule called aws which return false if string is other than 'foo'
Note: CamelCase in function name validateAws
2) Create service provider class and register it
We add our new validation rule into app\Providers\AppServiceProvider.php that comes by default with Laravel. You may create a validator instance manually using the Validator facade.
Don't forget to add 'use App\Services\Validation\CustomValidator;' to be able to, well, use it within the class.
3) Use created custom validation
Usage of custom validator rule to check name is foo only.
Well, fortunately, laravel is very flexible as it allows you to build your custom validation rules and with different ways. we've added our rule uk_phone as a second rule to the field phone – in addition to being required, it will also check out the string is 'foo' only.
Bottom Note
Now the notable thing here is how we set the name of the methods.
1) All validation rule method names must have the validate prefix. The rest of it must be in CamelCase (without spaces).
2) Validation rule will be in lowercase of what the method is named (without validation prefix) and each word will be separated by an underscore.
You can take the ride with more detail about laravel 5.2 validation, Displaying The Validation Errors, Custom Error Messages.
So here we go, that's recipe about how we can create and use custom validation rules with Laravel 5.