Search code examples
phplaravellaravel-requestlaravel-form

FormRequest not populating $request->old() after validation failure


My request object isn't receiving old() data when the form fails validation. I am receiving the error messages, but none of the old input data.

I've read a few solutions on similar questions that reference making changes to the redirect on the controller, but that won't solve this problem because the redirect is done by the function referenced below within the FormRequest Class and not the Controller.

Has anyone else experienced this same issue? I've upgraded my instance as I read a few forums that referenced existing bugs, but the issue still exists.

Any help would be appreciated.

Versions: I've tried this on laravel 5.4, 5.7 and 5.8 and none of them are rendering old data.

How I'm doing Request Validation The validation is being done via the standard Requests file that extends FormRequest.

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CustomerManagementRequest extends FormRequest {
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize() {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules() {
        return [
            'first_name' => 'required|min:2',
            'last_name' => 'required|min:2',
            'email' => 'required|min:4',
        ];
    }

}

How I'm trying to access the old data on my view:

value="{{old('first_name')}}"

The Redirect and validation being done in FormRequest The redirect is being done via the Laravel standard FormRequest class.

protected function failedValidation(Validator $validator)
{
    throw (new ValidationException($validator))
                ->errorBag($this->errorBag)
                ->redirectTo($this->getRedirectUrl());
}

The Validator Response: This is what I see when I vardump the above function. It does have my form data.

Validator {#629 ▼
  #data: array:11 [▼
    "_token" => "1ynKXxi551UBGbJq6ftLsW6VsClZzmbZdHiHIxyt"
    "active_from_date" => "04/04/2019 10:58 PM"
    "last_sync_date" => "04/04/2019 11:00 PM"
    "first_name" => "Pizza"
    "last_name" => "Dough"
    "email" => null
    "full_phone" => null
    "phone" => null
  ]
}

vardumping the old() parameter in the view returns an empty array []


Solution

  • I found the solution to this and thought I'd share it in case it helps anyone out there wracking their brains trying to figure a similar issue out.

    I had a custom middleware called ViewData that had a session()->save() command at the end of it which was saving over flashed data, thus causing my array of old inputs to be empty.

    Here is how I stepped through it for those of you debugging a similar issue.

    First I tried to create a manual validation in my controller to overrule an issue with FormRequest. I then followed the validator to the ShareErrorsFromSession middleware in the kernel.

    I realized I had a middleware that ran after this middleware (I call it ViewData) and that middleware stores some variables to session.

    protected $middlewareGroups = [
            'web' => [
    
                \App\Http\Middleware\EncryptCookies::class,
                \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
                \Illuminate\Session\Middleware\StartSession::class,
                //\Illuminate\Session\Middleware\AuthenticateSession::class,
                \Illuminate\View\Middleware\ShareErrorsFromSession::class,
                \App\Http\Middleware\ViewData::class,
                \App\Http\Middleware\VerifyCsrfToken::class,
                \Illuminate\Routing\Middleware\SubstituteBindings::class,
            ],
    

    I scrolled through and found a session()->save(). This save saved over the work being done by the withInput, essentially keeping old empty. I removed the save session (as it wasn't of value) and now old() contains the data expected.