Use fluent-style syntax to build Laravel validation rules
Example:
$rules = [
'id' => Rule::int()->required(),
'name' => Rule::string()->required()->minLength(3)->toString(),
'email' => Rule::string()->required()->email()->toArray()
]
- Strong typing
- Intellisence for available rules and parameters (if you use smart IDE, like PHPStorm)
- Hints about mistypings (if you use smart IDE, like PHPStorm)
Inline documentation:
Inellisence:
Custom validation rules
You can also use custom declared validations:
$rules = [
... => Rule::custom('foo')
]
Install the saritasa/laravel-fluent-validation
package:
$ composer require saritasa/laravel-fluent-validation
Add the FluentValidationServiceProvider in config/app.php
:
'providers' => array(
// ...
Saritasa\Laravel\Validation\FluentValidationServiceProvider::class,
)
Note: You can omit service provider registration, but then you must call ->toString() or ->toArray() on each builder. If service provider is registered, manual casting of rule to string or array is not necessary and default Laravel's Illuminate\Validation\ValidationServiceProvider::class can be removed from 'providers' array.
Root of your rule builder.
- Create fork
- Checkout fork
- Develop locally as usual. Code must follow PSR-1, PSR-2
- Update README.md to describe new or changed functionality. Add changes description to CHANGES.md file.
- When ready, create pull request