Validation Laravel 8 Laravel 9 Laravel 10 Laravel 11 Laravel 12

ValidationException in Laravel - Handling Validation Errors

This exception is thrown when form validation fails, typically when using the validate() method or Form Requests.

The Error

Error Message
The given data was invalid - ValidationException

Common Causes

  1. 1 Form data doesn't meet validation rules
  2. 2 Required fields missing from request
  3. 3 Invalid data format or type
  4. 4 API not handling validation response properly

Solutions

1

Display validation errors in Blade

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

Show field-specific errors

Blade
<input type="text" name="email" value="{{ old('email') }}">
@error('email')
    <span class="text-red-500">{{ $message }}</span>
@enderror
3

Handle validation in API responses

JavaScript
// Laravel automatically returns 422 with errors for API requests
// In your frontend:
axios.post('/api/users', data)
    .catch(error => {
        if (error.response.status === 422) {
            console.log(error.response.data.errors);
        }
    });
4

Customize validation messages

PHP
$request->validate([
    'email' => 'required|email|unique:users',
], [
    'email.required' => 'We need your email address.',
    'email.unique' => 'This email is already registered.',
]);

Need Help With Your Laravel Project?

I specialize in building custom Laravel applications, process automation, and SaaS development. Whether you need to eliminate repetitive tasks or build something from scratch, let's discuss your project.

Hafiz Riaz

About Hafiz

Senior Full Stack Developer. I build production software with Laravel, Filament, Vue, and AI integrations, and write about the real decisions behind shipping it.

Get in touch →