How to redirect back to form with input - Laravel 5

PhpFormsLaravelRedirectLaravel 5

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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 - Php

write old function on your fields value for example

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

Solution 3 - Php

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 - Php

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 - Php

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');

Solution 6 - Php

I handle validation exceptions in Laravel 5.3 like this. If you use Laravel Collective it will automatically display errors next to inputs and if you use laracasts/flash it will also show first validation error as a notice.


Handler.php render:

public function render($request, Exception $e)
{
    if ($e instanceof \Illuminate\Validation\ValidationException) {
        return $this->handleValidationException($request, $e);
    }

    (..)
}

And the function:

protected function handleValidationException($request, $e)
    {
        $errors = @$e->validator->errors()->toArray();
        $message = null;
        if (count($errors)) {
            $firstKey = array_keys($errors)[0];
            $message = @$e->validator->errors()->get($firstKey)[0];
            if (strlen($message) == 0) {
                $message = "An error has occurred when trying to register";
            }
        }

        if ($message == null) {
            $message = "An unknown error has occured";
        }

        \Flash::error($message);

        return \Illuminate\Support\Facades\Redirect::back()->withErrors($e->validator)->withInput();
    }

Solution 7 - Php

Laravel 5:

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

for back only:

return back()->withInput();

Solution 8 - Php

You can try this:

return redirect()->back()->withInput(Input::all())->with('message', 'Something 
went wrong!');

Solution 9 - Php

For Laravel 5

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

For Laravel 6, 7 and 8

return back()->withInput();

Docs:

Solution 10 - Php

$request->flash('request',$request);

<input type="text" class="form-control" name="name" value="{{ old('name') }}">

It works for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanny KoppingView Question on Stackoverflow
Solution 1 - PhpDanny KoppingView Answer on Stackoverflow
Solution 2 - PhpVishal RambhiyaView Answer on Stackoverflow
Solution 3 - PhpManiruzzaman AkashView Answer on Stackoverflow
Solution 4 - PhpAditya TomarView Answer on Stackoverflow
Solution 5 - PhpRashed RahatView Answer on Stackoverflow
Solution 6 - PhpRavView Answer on Stackoverflow
Solution 7 - PhpLuca C.View Answer on Stackoverflow
Solution 8 - PhpRashed RahatView Answer on Stackoverflow
Solution 9 - PhpGassView Answer on Stackoverflow
Solution 10 - PhpTuấn Anh Trần VănView Answer on Stackoverflow