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

Posted 8 months ago by EarlyReflexions

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.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.