How to work with data validation in Laravel

Asked

Viewed 1,297 times

1

I’m currently working with Laravel 5.5, however, this question applies to "all versions" because of its generalisation.

My question is: am using $this->validade to validate data coming from form. What would be the recipe to choose between using $this->validate or Form Request Validation?

I once thought of using Form Request Validation where it is necessary to carry out the validation in other methods, in order not to run the risk of by an oversight end up writing divergent rules in different methods.

Another thing, we might consider "impractical/inappropriate" working on all methods with Form Request Validation?

1 answer

4

For a better understanding of the differences in the method validate and of Form Request Validation I will tell you a little bit of each, according to what the official documentation quotes, at the end the main difference between them and which one would be more worth using.

In the method validate provided by the object, If the validation rules are approved, your code will continue running normally; however, if the validation fails, an exception will be posted 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, while a JSON response will be sent to AJAX requests.

Method validate in the function within the Controller

public function store(Request $request)
{
    $validatedData = $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);
}

We simply pass the desired validation rules for the method validate (at runtime of the called function). If the validation fails, the appropriate response will be generated automatically. If the validation is passed, the Controller will continue running normally.

Form Request Validation

It is used in more complex validation scenarios. The 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 directory app/Http/Requests. The validation rules shall be added to the method rules. Example:

public function rules()
{
    return [
        'title' => 'required|unique|max:255',
        'body' => 'required',
    ];
}

So, how are the validation rules executed? All you need to do is type the request into your controller’s method:

public function store(StoreBlogPostRequest $request)
{
    // The incoming request is valid...
}

The Form Request received is validated before the controller method is called, which means that you do not need to clutter your controller with any validation logic as 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 that they are available for display. If the request was an AJAX request, an HTTP response with a 422 status code will be returned to the user, including a JSON representation of the validation errors.

Thus the main difference between them is the time when the validation occurs, being one within the method in the Controller and one before the Controller be called. But which one is worth using more?

It all depends on the scope and size of your project, if there are many forms with similar/equal validation rules, it will be worth building a Form Request Validation, otherwise it will be worth using the methods validate within the Controller.

As to whether the use of Form Request Validation in all methods, I imagine that by being called before the Controllers run, make the code as a whole cleaner, organized and fast. But again falls into the "depends", you would build a Form Request for each form? Could they be used in various functions? Otherwise, I think the validate would be more appropriate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.