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.
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
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',
];
}
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.
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.
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!
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.
Blade components are reusable pieces of UI, like mini-templates, which you can use across multiple views.
Yes! You can easily add Tailwind CSS classes or custom CSS to your input-error component.
No, you can validate directly inside the controller too, but using Form Requests keeps the controller clean.
By default, Laravel shows the first error. If you want to display multiple, you can modify the Blade component to loop through errors.
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: