4

TLDR;

(In Laravel 5.8) Upon validation error, how can I return to a specific view with all input and the errors?

I'm creating a multistep form in which I need to validate the input on each step. Therefore, I have created the first page as a create and then the subsequent pages to update.

On the second page, when the validator fails, I am returning the specific view as I want it to go to this partial and not the beginning of the form. I am also returning the errors and the input on fail.

Controller:

if ($validator->fails()) {
    return view('partials.question_02_' . $type)
        ->with('type', $type)
        ->with('id', $id)
        ->withErrors($validator)
        ->withInput($request->all());
}

Here is my validation logic:

$validator = Validator::make($request->all(), [
    'email'               => 'required',
    'first_name'          => 'required',
    'last_name'           => 'required',
    'phone'               => 'required',
]);

With this, I am able to go back to the specific view and with the validation errors. However, I am not able to receive the old input.

The following in Blade will show that there is an erro (the error class appears), however, it will not show the old input.

<input class="@if ($errors->has('first_name')) error @endif" 
type="text" name="first_name" placeholder="First Name" value="{{ old('first_name') }}">

*note, I only put it on two lines for legibility here

What is weird is that if I die and dump the request, I can see all of the input:

dd($request->all());

However, if I try to get it from the session, I don't see any of the input:

@if($errors)
    {{var_dump(Session::all())}}
@endif

Any idea why {{ old('first_name') }} isn't working in this case?

Thanks!

Brad Ahrens
  • 4,864
  • 5
  • 36
  • 47
  • 1
    Can you add your validation logic? – Aditya Thakur May 08 '19 at 17:44
  • Also you don't need to pass any parameters to `->withInput();` – Aditya Thakur May 08 '19 at 17:47
  • @AdityaThakur, I have added the validation logic. What do you think? – Brad Ahrens May 08 '19 at 17:52
  • @AdityaThakur, when I added ->withInput(); without the parameters, it failed, giving me an Undefined offset: 0 error – Brad Ahrens May 08 '19 at 17:53
  • 1
    instead of return view(), you need return redirect(); return view will load up a fresh page. – Aditya Thakur May 08 '19 at 18:05
  • @AdityaThakur, I have tried return redirect()->back() and return back(). Unfortunately, both of these return me to step 1 instead of step 2, which I am on. I have also tried return->redirect()->view(), but that threw an error (Illuminate\Routing\Redirector::view does not exist.) – Brad Ahrens May 08 '19 at 18:08
  • your second page is generated using a route? – Aditya Thakur May 08 '19 at 18:18
  • I was generating it with the following code after I submitted the form: return view('partials.question_02_' . $leads->type). However, I am now trying to see if I redirect to this page instead of returning it will work. Maybe this will keep it at the second step on redirect->back() – Brad Ahrens May 08 '19 at 18:27
  • 1
    you can redirect to the controller method which is generating your second page `return redirect()->action('LoginController@secondPage');` – Aditya Thakur May 08 '19 at 18:30
  • 1
    Excellent! It worked! I had to add a few parameters to the "action" tag, but it worked! :) :) Thanks so much @AdityaThakur ! If you'd like, you can respond and I will mark this as the correct answer. – Brad Ahrens May 08 '19 at 18:40

1 Answers1

2

You're Returning a view return view() instead you need to return redirect return redirect()as a return view will generate a fresh page and old() will not work.

So instead of

return view('partials.question_02_' . $type)

Use

return redirect()

&in your case

redirect()->action('LoginController@secondPage'); //you controller and method handling your second page 
Aditya Thakur
  • 2,562
  • 2
  • 10
  • 21