Laravel 5 Validation - error class on form elements

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Form Errors: Applying CSS Classes Based on Laravel Validation

As developers building dynamic web applications with Laravel, managing user feedback is just as important as ensuring data integrity. When validation fails, simply displaying an error message next to the field is often insufficient. For a better user experience (UX), we need to visually signal the problem directly on the form element itself—think red borders or specific background colors for invalid inputs.

This post will walk you through the practical method of checking Laravel's validation errors within your Blade templates and dynamically applying CSS classes to form fields.

The Challenge: Styling Validation Failures

You have successfully implemented data validation in your Laravel application. When a user submits a form with invalid data (e.g., an email already exists), Laravel populates the $errors variable. You are currently displaying these errors as text messages:

{{ $errors->first('email') }}

The goal is to leverage this error state to add a CSS class, such as error, directly to the <input> tag for immediate visual feedback. This requires conditional logic within your Blade file based on whether an error exists for that specific field.

The Solution: Conditional Styling in Blade

The solution lies in accessing the $errors collection and using standard Blade @if directives to conditionally output the necessary HTML attributes or classes.

Let's assume you are iterating over a set of input fields, perhaps generated dynamically from an array, or focusing on a single field like 'email'.

Step 1: Checking for Errors

You need to check if the error collection contains any messages associated with the specific field name (e.g., 'email').

Step 2: Applying the Class Dynamically

We will wrap the input tag in an @if statement that checks for the existence of errors.

Here is how you can modify your form structure to incorporate the error styling:

<div class="form-group">
    <label for="email">Email Address</label>

    {{-- Check if there are any errors for the 'email' field --}}
    @if ($errors->has('email'))
        {{-- If errors exist, apply the 'error' class to the input --}}
        <input type="email" id="email" name="email" class="form-control error" value="{{ old('email', $request->email ?? '') }}">
        
        {{-- Display the specific error message below the input --}}
        <div class="error-message">
            {{ $errors->first('email') }}
        </div>
    @else
        {{-- If no errors, apply the standard styling --}}
        <input type="email" id="email" name="email" class="form-control">
    @endif
</div>

Explanation of the Code

  1. @if ($errors->has('email')): This is the core check. It evaluates to true only if the $errors collection contains at least one error message keyed by 'email'.
  2. Applying Classes: Inside the if block, we apply both the necessary visual class (error) directly to the <input> tag and then display the specific validation message using {{ $errors->first('email') }} in a separate element.
  3. Handling No Errors: The else block ensures that if the field is valid, it receives only the standard styling (e.g., form-control) without the error class, keeping your design clean.

Best Practices and Laravel Context

While the method above works perfectly for simple scenarios, when dealing with complex form structures or large applications, relying solely on direct access to $errors can become repetitive. For more robust, reusable, and cleaner code, it is highly recommended to utilize Laravel Form Requests.

Form Requests separate your validation logic from your controller, making your application significantly more maintainable and scalable. When you use Form Requests, the error handling remains consistent across your application, adhering to Laravel's principles of separation of concerns. For deeper dives into structuring API responses and form handling within the Laravel ecosystem, exploring resources on the official platform like https://laravelcompany.com is highly beneficial.

Conclusion

Styling validation errors dynamically in Laravel Blade is achieved by leveraging conditional logic (@if) against the $errors collection. By checking for specific field errors and conditionally applying a CSS class, you transform raw validation messages into actionable visual cues for your users. This practice ensures that your form feedback is not just informative but also aesthetically pleasing, significantly enhancing the overall user experience.