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

EarlyReflexions's avatar

Is there a way to keep old() from flashing until I want it to?

Heya !

I'm working on a "step by step" registration scenario, where the form fields are validated at each step but the storage only occurs at the end of the steps.

RegistrationController.php:

[...]
    public function firstStep() { // GET step 1
        return view('step1_view');
    }
    public function firstStepRecord(Request $request) { // POST step 1
        $this->validate($request, [
        // some validation
        ]);
        return redirect(route('second_step_route'));
    }
    public function secondStep() { // GET step 2
        return view('step2_view');
    }
    public function secondStepRecord(Request $request) { // POST step 2
        $this->validate($request, [
        // some validation
        ]);
        return redirect(route('third_step_route'));
    }
[...]

During validation of step 1, old() helper works like a charm.

BUT ! If I've reached the second step, old() - as it is supposed to do - has been flushed, and, if I try go to back to "first_step_route", old() fields are empty...

Right now I'm using something that looks gross - it works but it's ugly :

RegistrationController.php:

public function firstStep() { // GET step 1
    if(session('step_1') {
        $fromOtherSteps = session()->all();
    }
    else  {
        // :scary: having to set up empty indexes for the view, using the default value for old()
        $fromOtherSteps = ['all' => '', 'my' =>'', 'freaking' => '', 'fields' => '',];
    }
        return view('step1_view', compact('fromOtherSteps'));
}
public function firstStepRecord(Request $request) { // POST step 1
    $this->validate([ . . . ]);

    $request->session()->put([ // :scary: PART II
        'step_1' => true,
        'all' => request('field'),
        'my' => request('field'),
        'freaking' => request('field'),
        'fields' => request('field')]);
[...]
}

With something like that in the "step1_view.blade.php" :

<input value="{{ old('my_field', $fromOtherSteps['my_field']) }}" >

I'd be more confortable just keep "old()" in step 1, 2 and 3 and of course remembering to flush it when the actual db storage occurs...

Is there a way to keep the old() helper from flashing? or a more elegant workaround?

Thanks a lot in advance for your time.

0 likes
5 replies
tykus's avatar

You could use your own session key to store the multistep form data instead. Then check either your-key or old to repopulate the form in the event of stepping backwards (if both have data for an input, then prefer for old since it will be the result of a validation failure and your session contents would be out of date).

skimuli's avatar

You can use $request->session()->reflash(); this will not flush the session data. Or $request->session()->keep(['old']);

newbie360's avatar
Level 24

if you are talking about reuse the create and edit view

<input value="{{    old('my_field',       $fromOtherSteps['my_field']  ?? ''      )    }}" >

first time load the create view show the form, will use the highlighted

old('my_field', $fromOtherSteps['my_field'] ?? '' )

input something and submit, but validation fail, so redirect back, now use

old('my_field', $fromOtherSteps['my_field'] ?? '' )

change the input and submit, now pass the validation and redirect back edit view

old('my_field', $fromOtherSteps['my_field'] ?? '' )

is this clear ?

1 like
tykus's avatar

Problem with reflashing is any new validation failures will overwrite old.

EarlyReflexions's avatar

@tykus : that would be the point not to have a session key and old()

@newbie360 that's more elegant thanks! I still need to use a $fromOtherSteps variable but it's something I'll use! Thanks!

@skimuli : Sounds like a solution indeed... Do I need to reflash at each controller call?

Please or to participate in this conversation.