How to redirect back to form with input - Laravel

2019-01-13 17:32发布

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

6条回答
爷、活的狠高调
2楼-- · 2019-01-13 18:01

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

Hope, it might work in all case, thanks.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-13 18:02

this will work definately !!!

  $v = Validator::make($request->all(),[
  'name' => ['Required','alpha']
  ]);

   if($v->passes()){
     $ab = $request->name;
     print_r($ab);
   }
   else{
     //this will return the errors & to check put "dd($errors);" in your blade(view)
     return back()->withErrors($v)->withInput();
   }
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-13 18:05

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());
查看更多
趁早两清
5楼-- · 2019-01-13 18:07

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();
    }
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-13 18:08

Laravel 5:

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

for back only:

return back()->withInput();
查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-13 18:15

write old function on your fields value for example

<input type="text" name="username" value="{{ old('username') }}">
查看更多
登录 后发表回答