Laravel Validation

Facebooktwitterredditpinterestlinkedinmailby feather

Laravel provides a different way to validate the applications incoming data.

Laravel base controller class uses a ValidatesRequests that provides an easy method to validate incoming HTTP request with many powerful validation rules.

Defining the Routes

The following routes are defined in our app/Http/routes.php file:

The GET route will present a form for the user which creates a new post and the POST route that will store the new post in the database.

Creating the Controller

A simple controller which handles these routes. We will leave the store method empty for now:

The Validation Logic

Now we can fill our store method with the logic that validates the new post.

Applications base controller will be located in (App\Http\Controllers\Controller) class.

It provides an easy way to validate method in all of our controllers. 

An incoming method requests and a validation rule is accepted by the validate method.

If the validation rules pass, our code will keep executing normally.

An exception will be thrown, and the error response will automatically sent to the user only if the validation fails.

We pass the incoming HTTP request and desired validation rules into the validate method.

The proper response will automatically be generated only if the validation fails.

And if the validation passes, our controller will continue executing normally.

First Validation Failure

The first validation failure stops running validation rules on an attribute.

Assign the bail rule to the attribute:

In the above example, if the required rule on the title attribute fails, then the unique rule will not be checked.

Rules will be validated in order they are assigned.

Displaying The Validation Errors

The $errors variable will be a reference to Illuminate\Support\MessageBag.

Note: 0

The $errors variable to the view by the Illuminate\View\Middleware\ShareErrorsFromSession middleware that is provided by the web middleware group. 

This middleware is activated an $errors variable which will always available in our view, it allow us easily assume the $errors variable that is always defined.

The @error Directive

The @error Blade directive checks if validation error messages exist for a given attribute.

A @error directive, we echo the $message variable that display the error message:

Form Request Validation

Creating Form Requests

 Form requests are custom request classes which contain validation logic.

To create a form request class, we use the make:request Artisan CLI command:

The generated class will be located in the app/Http/Requests directory.

It will be created when we run the make:request command only if the app/Http/Requests directory does not exist.

The incoming form request is validated before calling the controller method, it means that we do not need to disarrange our controller with any validation logic:

A redirect response will be generated to send the user at the previous location only if the validation fails. The errors will be flashed to the session, so they are available for display.

A HTTP response with 422 status code then it will be returned to the user by including a JSON representation of the validation errors if the request was an AJAX request.

Authorizing Form Requests

The form request class contains an authorize method.

We can check the authenticated user has the authority to update a given resource.

The base Laravel request extends by all form requests, we use the user method to access the authenticated user.

For callingroute method in the above example, the requested method access the URI parameters which are specified on the route, like as the {comment} parameter.

Route::post(‘comment/{comment}’);

If the authorize method returns false, then an HTTP response with the status code 403, it will be automatically return, and our controller method will not execute.

In the above example, the authorization logic simply returnstrue from the authorize method.

Working With Error Messages

The errors method on a Validator, an Illuminate\Support\MessageBag has several methods for working with error messages.

Retrieving the First Error Message

We use the first method for retrieving first error message of the given field:

Retrieving All Error Messages

We use the get method to retrieve an array of all the messages to a given field:

Retrieving All Error Messages

We use the all method to retrieve an array of all messages for all the fields:

Facebooktwitterredditpinterestlinkedinmailby feather