How to redirect back to form with input - Laravel 5

151,238

Solution 1

You can use the following:

return Redirect::back()->withInput(Input::all());

If you're using Form Request Validation, this is exactly how Laravel will redirect you back with errors and the given input.

Excerpt from \Illuminate\Foundation\Validation\ValidatesRequests:

return redirect()->to($this->getRedirectUrl())
                    ->withInput($request->input())
                    ->withErrors($errors, $this->errorBag());

Solution 2

write old function on your fields value for example

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

Solution 3

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.

Like for a name validation, this will be-

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

Now, you can get the value after submitting it if there is error with redirect.

return redirect()->back()->withInput();

As @infomaniac says, you can also use the Input class directly,

return Redirect::back()->withInput(Input::all());

Add: If you only show the specific field, then use $request->only()

return redirect()->back()->withInput($request->only('name'));

Update: Get more example and real-life demonstration of Laravel form input here - https://devsenv.com/tutorials/how-to-redirect-back-in-laravel-with-form-input-and-many-possible-ways

Hope, it might work in all case, thanks.

Solution 4

this will work definately !!!

  $validation = Validator::make($request->all(),[
  'name' => ['Required','alpha']
  ]);
  
   if($validation->passes()){
     print_r($request->name);
   }
   else{
     //this will return the errors & to check put "dd($errors);" in your blade(view)
     return back()->withErrors($validation)->withInput();
   }

Solution 5

You can use any of these two:

return redirect()->back()->withInput(Input::all())->with('message', 'Some message');

Or,

return redirect('url_goes_here')->withInput(Input::all())->with('message', 'Some message');

Share:
151,238
Danny Kopping
Author by

Danny Kopping

Updated on July 05, 2022

Comments

  • Danny Kopping
    Danny Kopping almost 2 years

    How do I redirect back to my form page, with the given POST params, if my form action throws an exception?

    • whoacowboy
      whoacowboy almost 9 years
      You know I had to upvote your question only because you asked and answered it at the same time.
    • Danny Kopping
      Danny Kopping almost 9 years
      Thought it'd be helpful for other folks. Couldn't find the solution so i dug through the source to see how it's handled with Form Validation automatically
    • whoacowboy
      whoacowboy almost 9 years
      I always appreciate it.
  • Danny Kopping
    Danny Kopping almost 9 years
    No, it does not maintain your input params
  • lesssugar
    lesssugar almost 9 years
    Sure, I ment this: return redirect()->back()->withInput(); It's just a handy macro.
  • nice_dev
    nice_dev about 8 years
    Thanks ! I am using Laravel 5.2 and this works for me too . Along with old() function in our blade template , we also need to use withInput() in our controller to make it work . Like => if($validate->fails()) return redirect("somepage")->withErrors($validate)->withInput();
  • Nitesh Verma
    Nitesh Verma over 7 years
    Any idea on how to do this for select
  • Josh Mountain
    Josh Mountain almost 7 years
    return back()->withInput();
  • Siempay
    Siempay over 6 years
    you should know that you will need this in your form <input type="text" name="username" value="{{ old('username') }}"> as @Vishal_Rambhiya responded
  • Naveen DA
    Naveen DA over 6 years
    @NiteshVerma you can use jquery like {{if(isset(old('select')){'$("select option[value=\''.old('select').'\']").attr("selected",true)'‌​}};
  • Gustavo Straube
    Gustavo Straube over 6 years
    Nice! I was passing $request->all() to this method but it's good to know that's not required.
  • Debiprasad
    Debiprasad about 6 years
    Is there any solution similar to this for Laravel 4.2?
  • Maxim Sagaydachny
    Maxim Sagaydachny over 4 years
    Your answer does not add anything new to previous answers. Also it adds confusion because you do not explain what 'url' is. Also you already made an answer to this question. Please delete one of your answers . You can edit your own previous post.
  • JuMa
    JuMa about 3 years
    how to do this on the select box ?