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

speedydan's avatar

old or session data

Hi!

So, I have a multi-step form, which validates at each stage and then adds the input into the session, before the data is processed on the final step.

my question is - because I want users to be able to step forward and back between the different stages of the form (there are 4 steps) is there a nicer way to show old input other than:

value="@if(!empty(old('full_name'))){{ old('full_name') }}@else{{ Session::get('step_1.full_name') }}@endif"

I'll need to show old('full_name') before the form step is submitted, but if a user comes back to this step, I'll need to pull the input from the session.

This just looks a bit janky and was wondering if someone would have a better way of approaching this.

Thanks in advance!

0 likes
2 replies
lostdreamer_nl's avatar
Level 53

You could do this inside your controller (having to much logic in a view is never a good thing)

  • Create 1 method in which you check session / request / old() for each of the form fields
  • Call that method in all the steps to get formData from session / request / old
  • Use $formData to populate your form.

So inside your controller you could have something like:

public function step1(Request $request) 
{
    // do stuff
    return view('step1', $data);
}
public function step2(Request $request) 
{
    // do stuff
    return view('step2', $data);
}

Which could be turned into something like:

public function step1(Request $request) 
{
    // do stuff
    $data['formData'] = $this->prepareFormData($request);
    return view('step1', $data);
}
public function step2(Request $request) 
{
    // do stuff
    $data['formData'] = $this->prepareFormData($request);
    return view('step2', $data);
}

private function prepareFormData($request) 
{
    $fields = ['all', 'form', 'fields', 'of', 'all', 'forms'];
    $data = [];
    foreach($fields as $field) {
        if($request->has($field) {
            $data[$field] = $request->get($field);
        } else if(\Session::has($field)) {
            $data[$field] = \Session::get($field);
        } else if(old($field)) {
            $data[$field] = old($field);
        }
    }
    return $data;
}

View:

value="@if(isSet($formData[$field])) {{ $formData[$field] }} @endif"
1 like

Please or to participate in this conversation.