How to Show Validation Errors in Blade Templates in Laravel
In Laravel Blade, you can show validation errors using the
$errors variable. Use @if($errors->any()) to check if errors exist and then loop through them with @foreach ($errors->all() as $error) to display each error message.Syntax
Use the $errors variable provided by Laravel to check and display validation errors in Blade templates.
@if($errors->any()): Checks if there are any validation errors.@foreach ($errors->all() as $error): Loops through all error messages.- Display each error inside HTML elements like
<div>or<li>.
blade
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endifOutput
<div class="alert alert-danger">
<ul>
<li>Error message 1</li>
<li>Error message 2</li>
</ul>
</div>
Example
This example shows a simple form with validation errors displayed above the form fields after submission if validation fails.
php,blade
<?php // routes/web.php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; Route::get('/form', function () { return view('form'); }); Route::post('/form', function (Request $request) { $request->validate([ 'name' => 'required|min:3', 'email' => 'required|email' ]); return 'Form submitted successfully!'; }); // resources/views/form.blade.php ?><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Form with Validation</title> <style> .alert { background-color: #f8d7da; color: #721c24; padding: 10px; border-radius: 5px; margin-bottom: 15px; } </style> </head> <body> <h1>Submit Your Info</h1> @if ($errors->any()) <div class="alert"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form method="POST" action="/form"> @csrf <label for="name">Name:</label><br> <input type="text" id="name" name="name" value="{{ old('name') }}"><br><br> <label for="email">Email:</label><br> <input type="email" id="email" name="email" value="{{ old('email') }}"><br><br> <button type="submit">Submit</button> </form> </body> </html>
Output
<h1>Submit Your Info</h1>
<div class="alert">
<ul>
<li>The name field is required.</li>
<li>The email must be a valid email address.</li>
</ul>
</div>
<form method="POST" action="/form">
<input type="hidden" name="_token" value="...">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" value=""><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" value=""><br><br>
<button type="submit">Submit</button>
</form>
Common Pitfalls
Common mistakes when showing validation errors in Blade include:
- Not checking
$errors->any()before displaying errors, which can show empty error blocks. - Forgetting to include
@csrfin forms, causing validation to fail silently. - Not using
old('field')to repopulate form inputs after validation fails. - Displaying errors outside the Blade template or not passing errors from the controller properly.
blade
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<!-- Wrong: Missing @csrf causes validation failure -->
<form method="POST" action="/form">
<!-- @csrf missing here -->
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
<!-- Right: Include @csrf and old() -->
<form method="POST" action="/form">
@csrf
<input type="text" name="name" value="{{ old('name') }}">
<button type="submit">Submit</button>
</form>Quick Reference
- Use
$errors->any()to check if errors exist. - Loop errors with
$errors->all()to display messages. - Always include
@csrfin forms to protect and enable validation. - Use
old('field')to keep user input after errors.
Key Takeaways
Use the $errors variable in Blade to check and display validation errors.
Always check $errors->any() before showing error messages to avoid empty blocks.
Include @csrf in your forms to ensure validation works correctly.
Use old('field') to repopulate form inputs after validation fails.
Display errors inside a styled container for clear user feedback.