如何使用输入重定向回形式 -​​ Laravel 5

inf*_*iac 49 php forms redirect laravel laravel-5

POST如果我的表单操作引发异常,如何使用给定的参数重定向回我的表单页面?

inf*_*iac 68

您可以使用以下内容:

return Redirect::back()->withInput(Input::all());
Run Code Online (Sandbox Code Playgroud)

如果您正在使用表单请求验证,这正是Laravel将重定向您的错误和给定输入的方式.

摘录自\Illuminate\Foundation\Validation\ValidatesRequests:

return redirect()->to($this->getRedirectUrl())
                    ->withInput($request->input())
                    ->withErrors($errors, $this->errorBag());
Run Code Online (Sandbox Code Playgroud)

  • 当然,我这样做:`return redirect() - > back() - > withInput();`它只是一个方便的宏. (10认同)
  • 你应该知道你需要以你的形式`<input type ="text"name ="username"value ="{{old('username')}}">`作为@Vishal_Rambhiya回复 (7认同)

小智 39

例如,在字段值上写旧函数

<input type="text" name="username" value="{{ old('username') }}">
Run Code Online (Sandbox Code Playgroud)

  • 关于如何为`select`做这个的任何想法 (2认同)

Man*_*ash 14

在您的HTML中,您必须使用value = {{ old('') }}.如果不使用它,您将无法获取值,因为会话将存储在其缓存中.

与名称验证一样,这将是 -

<input type="text" name="name" value="{{ old('name') }}" />
Run Code Online (Sandbox Code Playgroud)

现在,如果重定向出错,您可以在提交后获取该值.

return redirect()->back()->withInput();
Run Code Online (Sandbox Code Playgroud)

正如@infomaniac所说,你也可以Input class直接使用,

return Redirect::back()->withInput(Input::all());
Run Code Online (Sandbox Code Playgroud)

添加: 如果您只显示特定字段,请使用$request->only()

return redirect()->back()->withInput($request->only('name'));
Run Code Online (Sandbox Code Playgroud)

希望,它可能在所有情况下都有效,谢谢.


Adi*_*mar 7

这肯定会奏效!!!

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

   if($v->passes()){
     print_r($request->name);
   }
   else{
     //this will return the errors & to check put "dd($errors);" in your blade(view)
     return back()->withErrors($v)->withInput();
   }
Run Code Online (Sandbox Code Playgroud)


Rav*_*Rav 5

我在 Laravel 5.3 中像这样处理验证异常。如果您使用 Laravel Collective,它会自动在输入旁边显示错误,如果您使用 laracasts/flash,它还会显示第一个验证错误作为通知。


Handler.php 使成为:

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

    (..)
}
Run Code Online (Sandbox Code Playgroud)

和功能:

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();
    }
Run Code Online (Sandbox Code Playgroud)


Luc*_* C. 5

拉拉维尔 5:

return redirect(...)->withInput();
Run Code Online (Sandbox Code Playgroud)

仅用于背部:

return back()->withInput();
Run Code Online (Sandbox Code Playgroud)

  • `返回()-&gt;withInput();` (2认同)

Ras*_*hat 5

您可以使用这两个中的任何一个:

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

或者,

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