A better place for your validation rules in Laravel
Validation in Laravel is pretty easy. It basically gives you two options where to validate an incoming request:
- inside a controller action
- via a dedicated Form Request
This is a pretty simple yet powerful approach. You just ask the request to validate itself based on the passed rules. If the validation fails, the method creates a proper error message and send it back to the view. If the validation passes, it just moves on with the rest of the code inside the action.
This can get very expressive if you validate a long form with more than 10 fields; but there is something we can do about.
Form Request Validation
Here we need to create a Form Request object with a meaningful name.
$ php artisan make:request PostStoreRequest
Then, we define the rules in
rules() and pass the Form Request object to the action. We are now able to get the validated data out of
I am using In-Action validation for rapid development. I like to see the rules close to the code. In this phase I need to adjust them quickly without fiddling too much with several files.
In later state, I usually move them to a FormRequest class. The benefits are:
- cleaner and shorter action
- if the validation fails, the action will never hit
- the Form Request object has a dedicated place to authorize the action
But where light is, is also some darkness. The downsides are:
- a Form Request object for any action who handles user input
- your validation rules are spread and are hidden in all these files
While the first point is just like it is, the last point can be addressed.
Move the rules to the model
Since we try to validate and store a blog post, we also have a
BlogPost model. Fine! Now, just move the rules from where ever they are to the model and store them in a static variable:
You can even go one step further and combine two or more rules together.
Thats all, folks. Please let me know, what you think about this approach in the comments.