Laravel 12: Display Validation Errors Using Blade Components

I'm excited to walk you through how we can easily display validation errors in Laravel 12 using Blade Components. When I first started with Laravel, handling validation messages felt tricky. But trust me, once you know the right way, it becomes super simple and clean!

In this guide, I’ll show you step-by-step with examples how you can make your forms show validation errors nicely using Blade components.

Step-by-Step Guide: Display Validation Errors Using Blade Components in Laravel 12

Step-by-Step Guide: Display Validation Errors Using Blade Components in Laravel 12

 

Step 1: Setup Laravel 12 Project

First, make sure you have Laravel 12 installed. If not, you can create a new project like this:

composer create-project laravel/laravel laravel-validation-demo

 

Step 2: Create a Form Request for Validation

Using Form Request is the cleanest way to validate inputs.

Create a request:

php artisan make:request StoreUserRequest

Now, open app/Http/Requests/StoreUserRequest.php and add rules:

public function rules(): array
{
    return [
        'name' => 'required|string|max:255',
        'email' => 'required|email|unique:users,email',
        'password' => 'required|min:8',
    ];
}

 

Step 3: Create Blade Component for Validation Errors

Now let's create a Blade component to handle error messages beautifully.

php artisan make:component InputError

It will create two files:

  • app/View/Components/InputError.php

  • resources/views/components/input-error.blade.php

Inside resources/views/components/input-error.blade.php

@props(['field'])

@error($field)
    <div class="text-red-500 text-sm mt-1">
        {{ $message }}
    </div>
@enderror

Simple! This will now handle showing the error for any input field.

 

Step 4: Use the Validation Component in Your Blade Form

In your Blade form, you can now use it easily like this:

<form method="POST" action="{{ route('register') }}">
    @csrf

    <div>
        <label>Name:</label>
        <input type="text" name="name" value="{{ old('name') }}">
        <x-input-error field="name" />
    </div>

    <div>
        <label>Email:</label>
        <input type="email" name="email" value="{{ old('email') }}">
        <x-input-error field="email" />
    </div>

    <div>
        <label>Password:</label>
        <input type="password" name="password">
        <x-input-error field="password" />
    </div>

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

Whenever validation fails, the respective error message will show below the input field.

 

Step 5: Handle Validation in Controller

In your controller, use the form request like this:

use App\Http\Requests\StoreUserRequest;

public function store(StoreUserRequest $request)
{
    // Validation is already handled!
    
    // Save the user or do something else
    User::create($request->validated());

    return redirect()->back()->with('success', 'User created successfully.');
}

That's it!

 

Conclusion

In Laravel 12, using Blade Components for showing validation errors is not just clean, but super maintainable too.
It keeps your forms simple and your error messages consistent across the entire app. I highly recommend setting up a custom error component in every project you build with Laravel 12.

 

Frequently Asked Questions (FAQs)

  1. What are Blade components in Laravel 12?

    Blade components are reusable pieces of UI, like mini-templates, which you can use across multiple views.

  2. Can I customize the error component style?

    Yes! You can easily add Tailwind CSS classes or custom CSS to your input-error component.

  3. Is it mandatory to use Form Request for validation?

    No, you can validate directly inside the controller too, but using Form Requests keeps the controller clean.

  4. Can I display multiple validation errors for the same field?

    By default, Laravel shows the first error. If you want to display multiple, you can modify the Blade component to loop through errors.

  5. Does this method work in older Laravel versions?

    It mainly works from Laravel 7 upwards because Blade components were introduced then, but Laravel 12 gives better performance and structure.

 

For more detailed information, refer to the official Laravel documentation on components.


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS