How to display validation error messages in Laravel blade view

To display validation error messages in the Laravel blade view, loop through the $errors->all() which is available in all the views. The $errors variable is shared with all blade views by the ShareErrorsFromSession middleware. 

In this article, we are going to learn:

  1. How to perform validation in Laravel.
  2. How to display all the error messages in a single element.
  3. How to show an error under an input field.
  4. How to add custom error messages.

Perform validation

Let us take an example of a login form with email and password input fields. This input field data was validated in the action as given below.

$validated = $request->validate([
	'email' => 'required|email',
	'password' => 'required',
]);

When validation fails, the user will be automatically redirected back to their original location by Laravel itself.  All validation messages and request input data will be flashed to the session automatically. So previous input data and error messages can easily be accessed from view templates.

If you are developing APIs, then you can return error messages as JSON as given below.

$validator = Validator::make($request->all(), [
                            	'email' => 'required|email',
                            	'password' => 'required',
                            ]);

if($validator->fails()){
    return response()->json(['errors'=>$validator->messages()]);
}

Display errors in the blade template

To display errors in the blade template, you can use of $errors variable which is available in all blade views. This $errors variable is shared with all blade views by the ShareErrorsFromSession middleware, which is provided by the web middleware group. The $errors variable is an instance of Illuminate\Support\MessageBag.

Display all the messages in a single element

The $errors->any() method can be used to check whether any validation error message exists or not in the $error variable. Also, you can use $errors->all() to get all error messages.

Please have a look at the below code for a better understanding.

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

In the above example, we are listing all validation messages in an unordered list.

Show an error under an input field

 To show an error under an input field, you can do like:

<input type="email" class="form-control" id="email" name="email"/>
@if($errors->has('email'))
    <div class="text-danger">{{ $errors->first('email') }}</div>
@endif

 The $errors->has('email') method checks whether any error is associated with the email input field. And the $errors->first('email') method gives the first error message of that particular input field.

Using the @error Blade directive

The @error Blade directive can be used to check whether validation error messages are present for a specific attribute. You can also use the echo $message variable within a @error directive to show the validation message like:

<input type="email" class="form-control" id="email" name="email"/> 
@error('email')
    <div class="alert alert-danger">{{ $message }}</div>
@enderror

Customizing the messages

You can customize the validation error message for each validation rule in the application's resources/lang/en/validation.php file. 

Also, you can add a custom message as a second argument into the validate method as given below.

$validated = $request->validate([
                                    'email' => 'required|email',
                                    'password' => 'required',
                                ],
                                [
                                    'email.required' => 'Please enter email.',
                                    'email.email' => 'Please enter a valid email.',
                                    'password.required' => 'Please enter password.'
                                ]);

For deep knowledge see Laravel doc.

Conclusion

To display validation errors, you can use the $error variable which is available in all the views. The $errors->any() method checks whether any validation error message exists in the $error variable. 
Use $errors->all() to get all error messages and use $errors->first('fieldname') to get an error message specific to a particular input field. Also, you can use of @error Blade directive to check whether any validation error messages are present for a particular attribute.