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

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.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 29 (12 by maintainers)

Most upvoted comments

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.

Only happens in make:auth.

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

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: