To get a better understanding of the differences between the validate
and Form Request Validation
methods, I'll say a little bit of each, according to what the official documentation mentions, in the end the main difference between them and which one would be worth using.
In the validate
method provided by the object, If the validation rules are approved, your code will continue to run normally; however, if the validation fails, an exception will be thrown and the correct error response will be automatically sent to the user. In the case of a traditional HTTP request, a redirect response will be generated, whereas a JSON response will be sent to AJAX requests.
Method validate
in function within Controller
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
We simply pass the desired validation rules to the validate
method (at run time of the calling function). If the validation fails, the appropriate response will be generated automatically. If validation is passed, Controller
will continue to run normally.
Form Request Validation
Used in more complex validation scenarios. Form Request
are custom request classes that contain validation logic.
To create a class of Request
php artisan make:request StoreBlogPostRequest
The generated class will be placed in the app/Http/Requests
directory. The validation rules must be added to the rules
method. Example:
public function rules()
{
return [
'title' => 'required|unique|max:255',
'body' => 'required',
];
}
So, how do the validation rules run? All you have to do is type the request into your controller method:
public function store(StoreBlogPostRequest $request)
{
// The incoming request is valid...
}
The received% is validated before the controller method is called , which means you do not need to clutter your controller with any validation logic since it has already been validated!
If validation fails, a redirect response will be generated to send the user back to their previous location. Errors will also be displayed in the session so they are available for viewing. If the request were an AJAX request, an HTTP response with a status code of 422 will be returned to the user, including a JSON representation of the validation errors.
So the main difference between them is the time validation occurs, one being inside the method in Form Request
and the other before Controller
is called. But which one is worth more to use?
It all depends on the scope and size of your project, if there are many Controller
with similar / equal validation rules, it will be worth constructing a Form Request Validation , otherwise it will be worth using the methods forms
within validate
.
As to be impracticable / inappropriate to use Controller
in all methods, I imagine that because they are called before Form Request Validation
execute, they make the code as a whole cleaner, organized and faster. But does it fall back to "depends", would you build Controllers
for each Form Request
? Could they be availed in various functions? Otherwise, I think form
would be more appropriate.