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:
- How to perform validation in Laravel.
- How to display all the error messages in a single element.
- How to show an error under an input field.
- 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.
On datainfinities.com, Read articles all around JavaScript, React, Node.js, PHP, Laravel, and Shopify.
Related Blogs
How to create multiple where clause query using Laravel eloquent
Delete an element from an array in PHP
Laravel Cron Job Task Scheduling Tutorial
Get raw SQL query from Laravel Query Builder
Laravel Order by Pivot Table Field
Laravel Model Events and Observers Tutorial
The POST method is not supported for this route. Supported methods: GET, HEAD
How to solve error: array_merge() does not accept unknown named parameters
Explore All Blogs