How to work with data validation in Laravel

0

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

My question is: I'm 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 have come to the idea of using Form Request Validation whenever validation is required in other methods, so as not to risk inadvertently writing divergent rules in different methods.

Another thing, we might consider "impracticable / inappropriate" to work on all methods with Form Request Validation ?

    
asked by anonymous 20.10.2017 / 13:08

1 answer

1

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.

    
20.10.2017 / 14:55