Request Errors not accessible in blade (Laravel 5.2)

后端 未结 2 1256
没有蜡笔的小新
没有蜡笔的小新 2021-02-20 16:08

It had been many months since I\'m using laravel but never faced such problem.

I have made a simple Request class to validate the the update user request which works fin

2条回答
  •  無奈伤痛
    2021-02-20 16:51

    You may want to use withErrors redirect, in case validation fails

        if ($validator->fails()) {
            return redirect()
                ->route('route.here')
                ->withErrors($validator)
                ->withInput();
        }
    

    Also, please check

      \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    

    is there in 'web' middleware in app/Http/Kernel.php

    so your kernel.php should look something like:

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],
        'api' => [
            'throttle:60,1',
        ],
    ];
    

    If that too doesn't work, you can move the

      \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    

    to the global middleware. (just to try. I won't suggest)

    ** Make sure sessions work. To have errors returned or to flash messages to the browser, you need to have a session running.

    From 5.2, sessions only start if you specify that the route should use the 'web' middleware (which is already done by you in the routes.php).

    And, From 5.2.28, web middleware is automatically included in all routes, you can see this in the RouteServiceProvider. so, we don't want to specify a 'web' middleware to the routes.php or in controller unless we have a custom middleware. But, not sure this caused the problem.

提交回复
热议问题