Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

domdafonte's avatar

FormRequest not populating $request->old() upon failure

My forms aren't receiving the old() data when the form fails validation. The validation is being done via FormRequest as is standard in Laravel.

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

Has anyone else experienced this? If so how did you resolve this?

My Request:

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',
    ];
}

}

failed Validation (seems to be responsible for redirecting the data back with errors...

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

The Validator Response:

Validator {#629 ▼
  #failedRules: []
  #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
    "location" => array:3 [▶]
    "locationtitle" => null
    "action" => "cust"
  ]
}
0 likes
17 replies
ftiersch's avatar

Do your sessions work correctly? The old values are stored in the session so if they are not configured properly the values would be lost during the redirect

tykus's avatar

Are these api middleware routes or web (i.e. is there a Session)?

domdafonte's avatar

@FTIERSCH - Thanks for the follow up. My sessions are working properly. I’m actively using session data in my application.

domdafonte's avatar

@TYKUS - This is coming from a web route and has active session data. My form is using session variables I already passed.

tykus's avatar

If you dump the Session, is the old key in there under the flash key; is it empty?

1 like
domdafonte's avatar

My session shows all my own defined session data and other data, but the old key is empty.

tykus's avatar

The old data is flashed to the session, so will be available for a single request only, can you check that there there is no redirect happening which means the data was there by since got removed?

Otherwise, if handling the ValidationException yourself, you will need to explicitly flash the old data using withInput() method., e.g.

return back()->withInput(request()->input());
domdafonte's avatar

@TYKUS - There is a redirect on my controller which goes to the main page.

But from what I could tell, That redirect isn’t hit because I use the requests file highlighted in the OP, which uses the FormRequests class and performs the redirect in the method presented.

I validated this by placing a vardump in my store controller. That vardump is never hit because the requests file has a failed validation and performs its own redirect.

ftiersch's avatar

There is no Requests file visible in the OP :)

1 like
domdafonte's avatar

@FTIERSCH - Thanks for highlighting that. I Just looked and looks like all of my code was stripped! I’ll try to resubmit. The text editor looked pretty basic so perhaps it was stripped. Will update the op again.

tykus's avatar

If you open your browsers Dev Tools Network tab (and Preserve Log), you can see how many (and which) redirects are occurring - they might be web server level redirects rather than application level.

Do you handle a ValidationException in app/Exceptions/Handler.php?

tykus's avatar

The old request data is flashed to the Session in the Illuminate\Foundation\Exceptions\Handler class which handles invalid ValidationExceptions. You should check that your exception is being handled by the invalid() method; if not then something else is handling the ValidationException and return its own response.

1 like
domdafonte's avatar

@TYKUS - Thanks. We are getting the error bag but not the old input that was valid.

Snapey's avatar

Did you check the redirects in the browser network tools? Thats the most likely issue.

domdafonte's avatar
domdafonte
OP
Best Answer
Level 2

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.

Please or to participate in this conversation.