0
0
Laravelframework~5 mins

Displaying validation errors in Laravel

Choose your learning style9 modes available
Introduction

Showing validation errors helps users know what they need to fix in a form. It makes the app easier and friendlier to use.

When a user submits a form with missing or wrong information.
When you want to guide users to enter data correctly.
When you want to prevent saving bad or incomplete data.
When you want to highlight specific fields that need correction.
Syntax
Laravel
@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
Use $errors->any() to check if there are any validation errors.
Use $errors->all() to get all error messages as an array.
Examples
Basic example showing all errors in a list inside a div.
Laravel
@if ($errors->any())
    <div class="error-messages">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
Show error message for a specific field, here for 'email'.
Laravel
@error('email')
    <div class="error">{{ $message }}</div>
@enderror
Show error next to the username input field with old input value preserved.
Laravel
<input type="text" name="username" value="{{ old('username') }}">
@error('username')
    <span class="text-danger">{{ $message }}</span>
@enderror
Sample Program

This example shows a controller method that validates user input for name and email. If validation fails, Laravel automatically redirects back with errors. The Blade view displays errors next to each field and also lists all errors at the bottom.

Laravel
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|min:3',
            'email' => 'required|email',
        ]);

        // Save user or other logic here

        return redirect()->back()->with('success', 'User saved!');
    }
}

// Blade view snippet (resources/views/user_form.blade.php):

/*
<form method="POST" action="{{ route('user.store') }}">
    @csrf

    <label for="name">Name:</label>
    <input type="text" id="name" name="name" value="{{ old('name') }}">
    @error('name')
        <div class="error">{{ $message }}</div>
    @enderror

    <label for="email">Email:</label>
    <input type="email" id="email" name="email" value="{{ old('email') }}">
    @error('email')
        <div class="error">{{ $message }}</div>
    @enderror

    <button type="submit">Submit</button>
</form>

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

Laravel automatically redirects back with errors if validation fails.

Use old('field') to keep user input after validation fails.

Showing errors near fields helps users fix mistakes faster.

Summary

Validation errors tell users what to fix in forms.

Use $errors->any() and $errors->all() to display errors.

Show errors near inputs and optionally as a list for better user experience.