Laravel - Message Toastr, on Error or Invalid Input Fields

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Elevating User Feedback: Converting Laravel Validation Errors into Toastr Notifications

As a senior developer working with the Laravel ecosystem, we often focus on building robust backend logic, but the final piece of the puzzle is crafting an excellent user experience (UX). Providing timely, clear, and non-intrusive feedback—whether it's a success message or a critical validation error—is crucial.

You’ve hit upon a very common scenario: successfully implementing notifications for actions (like 'success' or 'info') using Sessions flashed via redirects, but struggling to apply the same elegant pattern to form validation errors. You are currently rendering standard HTML <div> alerts for errors instead of leveraging a dedicated notification library like Toastr.

This post will walk you through diagnosing why your error toasts aren't firing and provide the correct architectural approach to seamlessly integrate Laravel validation errors with Toastr notifications.

The Diagnosis: Why Errors Don't Convert Automatically

Your current setup works perfectly for success messages because you are manually checking and displaying a message stored in the session:

@if(Session::has('message'))
    // ... toast logic here
@endif

This is explicit. However, when validation fails, your controller is returning an error state, and the view is explicitly looping through $errors to render raw HTML alerts:

@if(count($errors) > 0)
    @foreach($errors->all() as $error)
        <div class="alert alert-danger">{{$error}}</div> // This renders native HTML, bypassing Toastr.
    @endforeach
@endif

The key issue is that the validation errors are being handled by the view layer rendering raw HTML, completely bypassing your custom JavaScript logic that listens for session messages. To make Toastr handle these errors, we need to shift the responsibility of displaying all feedback—successes and errors—to the same unified mechanism: the Session.

The Solution: Centralizing Feedback via Session Flashing

The best practice when dealing with form submissions in Laravel is to use the session to hold all necessary feedback. Instead of rendering HTML alerts directly in your view for validation failures, you should capture those errors and flash them into the session along with any success messages. This allows a single script block to check the session and display any type of notification.

Step 1: Modifying the Controller Logic

In your controller method where you handle the form submission (e.g., store or update), instead of just returning a redirect, you need to structure your response to include both success messages and validation errors in the session.

Here is how you would modify your controller logic:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class EmployeeController extends Controller
{
    public function store(Request $request)
    {
        // 1. Validate the request
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email',
            'password' => 'required|min:8',
        ]);

        if ($request->has('submit')) {
            // Success case: Store data and flash a success message
            $employee = Employee::create($validated);
            Session::flash('message', 'Employee Information Created!');
            Session::flash('alert-type', 'success');

            return redirect('/admin/employees/show');
        } else {
            // Failure case: Validation failed. Flash the errors.
            // Laravel automatically populates $errors with validation failures.
            Session::flash('message', 'Please correct the errors below.');
            Session::flash('alert-type', 'error');

            // Important: Return the request back to the form view so errors are displayed
            return back(); 
        }
    }
}

Step 2: Updating the View (Removing Manual Error Display)

Now, in your Blade file (messages.blade.php), you should remove the manual loop that displays the <div> alerts for validation errors. You will rely solely on checking the session data to trigger Toastr.

If you still need to display just the form-level error summary (which is often better handled by a dedicated component), you can keep it, but ensure the primary notification mechanism is Toastr. For this example, we focus on ensuring the errors are captured and not displayed as raw alerts.

Step 3: Ensuring Toastr Handles All Types

Your existing JavaScript setup for Toastr is already well-structured. By using Session::flash() with specific keys (message and alert-type), you ensure that whichever message is flashed, it can be correctly interpreted by the switch statement later in your script block.

<script>
    // ... (toastr scripts loaded above)

    @if(Session::has('message'))
        var type = "{{ Session::get('alert-type','info') }}"; // Defaults to info if alert-type is missing

        switch(type){
            case 'info':
                toastr.info("{{ Session::get('message') }}");
                break;
            case 'success':
                toastr.success("{{ Session::get('message') }}");
                break;
            case 'warning':
                toastr.warning("{{ Session::get('message') }}");
                break;
            case 'error':
               // This is where validation errors will now be caught!
               toastr.error("Validation Error: " + (Session::get('message') ?: 'An unknown error occurred.'));
               break;
        }
    @endif
</script>

Conclusion

By shifting the paradigm from rendering immediate HTML alerts for validation errors to flashing structured messages via the session, you achieve a cleaner, more centralized feedback loop. This approach ensures that all user-facing notifications—successes, warnings, and critical validation failures—are managed by your Toastr implementation, leading to a much more consistent and professional interface.

Remember, when building complex applications on Laravel, focusing on the flow of data through the session is a cornerstone of maintainable code. For deeper insights into structuring powerful application flows, exploring patterns found in frameworks like Laravel Company is highly recommended.