Framework: [5.2] No errors in session after validation fails

Created on 25 Jan 2016  路  29Comments  路  Source: laravel/framework

EDIT! I found the problem >> see second post

I was following Task List, Intermediate Task List and even 'Project Flyer' on Laracasts. There is a problem with displaying errors after a failing validation.

I created a very basic 'contactform', one index/form and one POST with validation. When validation is OK, the behavior is as expected. When validation fails, the errors are not in $errors. I do NOT use authentication, nor a database. I tried it with FormRequest and with basic $request/validation methods.

Besides that, (re)filling forms with value="{{ old('name') }}" also does not work...

And yes, I did put my routes in the web group middleware.

Routes snippet

Route::group(['middleware' => ['web']], function () {
    //Route::resource('contact', 'ContactController');
    Route::get('/contactform', 'ContactController@index');
    Route::post('/contact', 'ContactController@store');
});

Form snippet

@if (count($errors) > 0)
<ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
</ul>
@endif

<form action="{{ url('contact') }}" method="POST" class="form-horizontal">
  <input type="text" name="name" id="contact-name" class="form-control">
  <button type="submit">submit</button>
</form>

Controller snippet

public function index(Request $request)
    {
        var_dump($request->session()->all());
        return view('contact.index');
    }

    /**
     * Create a new task.
     *
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        var_dump($request->session()->all());
        $this->validate($request, [
          /* characters will fail the validation */
          'name' => 'required|integer|min:4',
        ]);

        dd($request->all());
    }

correct input gives me the dd() from the store method, so validation is working at a certain level. The problem must be in the redirect and session.

Most helpful comment

I noticed something: the web middlware is loaded twice when listing the routes by
php artisan route:list

I removed the
Route::group(['middleware' => 'web'], function () {
from my routes and now it works. The web middlware is only loaded once.

I am not sure when or what caused this behavior. On a new test project it didn't happen. I guess it has to do with the make:auth command, since that one I didn'd use in my test project.

All 29 comments

I found the 'problem' with debugging. PHPStorm strokethrough the ValidationException in the throwValidationException method, so:

The class Illuminate\Foundation\Validation\ValidatesRequests@throwValidationException throws a \Illuminate\Foundation\Validation\ValidationException

the class 'Illuminate\Foundation\Validation\ValidationException' says

@deprecated since 5.2.7. Use Illuminate\Validation\ValidationException.

But it is still used in Illuminate\Foundation\Validation\ValidatesRequests

When I add 'use Illuminate\Validation\ValidationException;' on top of that same file I get the correct error message in my view!

(I am not able to edit/propose etc, sorry)

Which Laravel version?
Have you tried composer update to get the last fixes?

@arilot: found in version 5.2.11, and yes

That deprecated class is fine. It extends a non-deprecated one, but we throw the deprecated one for BC reasons.

What exactly is the issue you're having?

I don't see a bug here?

  1. When validation fails, the errors are not in $errors
  2. (re)filling forms with value="{{ old('name') }}" also does not work

debugging shows indeed that the deprecated Exception is thrown but when I use the recommended one it everything works as expected, old values, form errors etc...

I can't oversee the whole process (Invalid input -> error in view) right now, but it seems like somewhere the Exceptions get 'weighted'

Illuminate\Foundation\Validation\ValidationException

frustrates the process (or the transition to errors), and

Illuminate\Validation\ValidationException

does not!

Can confirm here. I just added my routes to the web middleware and now my error bag is empty even though form validation fails. Laravel 5.2.12

Facing this issue in Laravel 5.2.15
@ciliehub Did changing the ValidationException work for you?

Facing this issue in Laravel 5.2.14
changing the ValidationException did not work,
removing \Illuminate\Session\Middleware\StartSession from web middleware in Kernel worked

Hmm I had to do the same thing as @rflipo-dev to get it working on my end. I had no errors or success messages getting returned to my session.

How does that impact the rest of my setup?

Thanks for getting back to me @rflipo-dev

I'm experiencing the same thing with Laravel 5.2.26 running on the latest version of Homestead.

No errors appear when validation fails. My routes are in the web middleware group. The only way I can seem to get this to work is by moving \Illuminate\Session\Middleware\StartSession::class, from the web middleware group into the global stack.

I noticed something: the web middlware is loaded twice when listing the routes by
php artisan route:list

I removed the
Route::group(['middleware' => 'web'], function () {
from my routes and now it works. The web middlware is only loaded once.

I am not sure when or what caused this behavior. On a new test project it didn't happen. I guess it has to do with the make:auth command, since that one I didn'd use in my test project.

I can confirm the exact same behavior as @cwansart, the web middleware gets doubled when doing php artisan make:auth and it causes the errorbag to be empty on validation (probably other stuff too?).

EDIT: this seems like a better answer. Should make:auth not add the web middleware ?

I can confirm the above. Running php artisan route:list shows that the web middleware is loaded twice after running php artisan make:auth. Removed the group middleware declaration from routes.php solved the problem for me.

I can confirm the same behavior on my system. As stated on https://laravel.com/docs/5.2/middleware#middleware-groups, the web group is automatically assigned to routes in routes.php. Thus, loading it explicitly in routes.php does not make sense.
As a consequence, the quickstart examples should probably be updated, since their routes files include the web middelware again:

@vis-bp The routes.php and RouteServiceProvider are consistent, you know.

@hiendv yes, routes.php and RoutServiceProvider are, but the code for the quickstart examples is wrong!

@hiendv sure, but this appears when you do make:auth. This is not on the user, this should be corrected, no?

Only happens in make:auth.

@hiendv oh, I didn't catch that. It officially came out with 5.2.29 a a day ago. I should be fixed now! :) :+1:

Just upgraded laravel to v5.2.30 still the same problem exist. i have to follow the suggestion to move:
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
from $middlewareGroups to $middleware inside Kernel.php or else $errors has no content upon validation if it has validation error

NOT A BUG Your dd in the store method prevents the session getting saved.

Any other "bugs" that you are seeing are probably caused by your having the same middleware enabled more than once by mistake,

Was this page helpful?
0 / 5 - 0 ratings