1

I'm using Laravel 7 with a validator facade to validate a form submission. Validation is working correctly; however I don't see where to access the old form information to re-populate the fields that the user already filled out. I can see the errors passed by ->withErrors(), but I don't see any input from ->withInput().

This is in my controller method: // validate the responses $validator = Validator::make($request->all(), [ 'question_1' => 'required', 'question_2' => 'required', 'question_3' => 'required', 'question_4' => 'required', 'question_5' => 'required', ]);

    if ($validator->fails()) {
        return redirect('/')
            ->withErrors($validator)
            ->withInput();
    }

However, when I debug the variables in the template, I don't see the old input data:

@if($errors->any())
        {{ dd(  get_defined_vars()['__data'] ) }}

    @foreach ($errors->all() as $error)
        {{ $error }}<br/>
    @endforeach
@endif

array:4 [▼
  "__env" => Illuminate\View\Factory {#290 ▶}
  "app" => Illuminate\Foundation\Application {#2 ▶}
  "errors" => Illuminate\Support\ViewErrorBag {#351 ▶}
  "questions" => Illuminate\Database\Eloquent\Collection {#1319 ▶}
]

In the above debugging output, I get the errors key with my error messages, but I don't see the old input anywhere in the variables. The questions key is the collection of database results passed to the template, not the old input.

How do I access the old form input that a user submitted, after a validation failure?

user151841
  • 17,377
  • 29
  • 109
  • 171

1 Answers1

1

This is smiler question and ther is useful answers : How to redirect back to form with input - Laravel 5 You can use :

return redirect()->back()->withInput(Input::all())

In your HTML you have to use value = {{ old('') }}. Without using it, you can't get the value back because what session will store in their cache.

<input type="text" name="name" value="{{old('name') }}" />