How to redirect back to form with input - Laravel 5
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');
Danny Kopping
Updated on July 05, 2022Comments
-
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 almost 9 yearsYou know I had to upvote your question only because you asked and answered it at the same time.
-
Danny Kopping almost 9 yearsThought 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 almost 9 yearsI always appreciate it.
-
-
Danny Kopping almost 9 yearsNo, it does not maintain your input params
-
lesssugar almost 9 yearsSure, I ment this:
return redirect()->back()->withInput();
It's just a handy macro. -
nice_dev about 8 yearsThanks ! I am using Laravel 5.2 and this works for me too . Along with
old()
function in our blade template , we also need to usewithInput()
in our controller to make it work . Like =>if($validate->fails()) return redirect("somepage")->withErrors($validate)->withInput();
-
Nitesh Verma over 7 yearsAny idea on how to do this for
select
-
Josh Mountain almost 7 years
return back()->withInput();
-
Siempay over 6 yearsyou should know that you will need this in your form
<input type="text" name="username" value="{{ old('username') }}">
as @Vishal_Rambhiya responded -
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 over 6 yearsNice! I was passing
$request->all()
to this method but it's good to know that's not required. -
Debiprasad about 6 yearsIs there any solution similar to this for Laravel 4.2?
-
Maxim Sagaydachny over 4 yearsYour 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 about 3 yearshow to do this on the select box ?