Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

The form redirects back but no error messages appear in the Blade view

ErrorThe form reloads with old input but no error text

Short answer

Laravel flashes validation errors to the session and shares them as $errors through the web middleware group. If the route is outside that group, the redirect was built by hand without withErrors, the page reads a different error bag, or the form was sent by XHR, the view shows nothing. Check each in that order.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Put this line in the view that should show the errors. It prints the messages Laravel actually has.

    {{ json_encode($errors->all()) }}
  2. 2Match what you see, then jump to its fix

    1. 1The first-step line prints [] or the view fails with an undefined $errorsThe route is outside the web middleware groupFix
    2. 2You call Validator::make and redirect yourselfA manual Validator fails without withErrorsFix
    3. 3withErrors was given a second argument, but the view reads plain $errorsThe errors are in a named bagFix
    4. 4The browser shows no redirect; the network tab holds a JSON error listThe request was sent by XHR, so Laravel returned JSONFix

What does the error look like?

  • The form reloads with old input but no error text
  • $errors->any() is false after a failed validation
  • @error('email') never renders its block

Cause 1 of 4

The route is outside the web middleware group

1Check

The $errors variable is shared by the ShareErrorsFromSession middleware, which is part of the web group. Routes in routes/web.php get that group. The -v option of route:list shows the middleware of each route.

php artisan route:list -v

2Fix

Define the route in routes/web.php, which is assigned the web middleware group.

// routes/web.php
Route::post('/contact', [ContactController::class, 'store']);

3Verify

Submit the invalid form again and look at the first-step output.

# submit the form in the browser

Confirmed by laravel.com

Did this fix it?

Cause 2 of 4

A manual Validator fails without withErrors

1Check

Only the built-in validate() call redirects and flashes automatically. With a manual validator, you must pass the errors to the redirect.

grep -rn "Validator::make" app/

2Fix

Chain withErrors and withInput on the redirect.

if ($validator->fails()) {
    return redirect('/post/create')
        ->withErrors($validator)
        ->withInput();
}

3Verify

Submit invalid data. The first-step line now lists the messages.

# submit the form in the browser

Confirmed by laravel.com

Did this fix it?

Cause 3 of 4

The errors are in a named bag

1Check

A name as second argument stores the messages in a separate bag. The default $errors->any() then reports nothing.

grep -rn "withErrors(" app/

2Fix

Read the same bag name in the view, or drop the name.

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

@error('email', 'login')
    <div class="alert alert-danger">{{ $message }}</div>
@enderror

3Verify

The message for the named form is visible.

# reload the page after a failed submit

Confirmed by laravel.com

Did this fix it?

Cause 4 of 4

The request was sent by XHR, so Laravel returned JSON

1Check

For XHR requests Laravel returns a JSON response with the messages instead of redirecting.

# browser dev tools, Network tab: look at the response body of the POST

2Fix

Read the messages from the JSON response in your script, or submit the form normally.

// the response body holds the validation messages as JSON

3Verify

The script shows the messages from the response.

# reload and resubmit

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Keep every browser form route in routes/web.php.
  • Use @error next to each field so a missing message is easy to spot.
  • Name an error bag only when one page holds several forms.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.