简体   繁体   English

如何使用输入重定向回表单 - Laravel 5

[英]How to redirect back to form with input - Laravel 5

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

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.如果您正在使用Form Request Validation ,这正是 Laravel 将您重定向回错误和给定输入的方式。

Excerpt from \\Illuminate\\Foundation\\Validation\\ValidatesRequests :摘自\\Illuminate\\Foundation\\Validation\\ValidatesRequests

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

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

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

In your HTML you have to use value = {{ old('') }} .在您的 HTML 中,您必须使用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,正如@infomaniac所说,您也可以直接使用Input class

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

Add: If you only show the specific field, then use $request->only()添加:如果只显示特定字段,则使用$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更新:在此处获取 Laravel 表单输入的更多示例和实际演示 - https://devsenv.com/tutorials/how-to-redirect-back-in-laravel-with-form-input-and-many-possible-方法

Hope, it might work in all case, thanks.希望,它可能适用于所有情况,谢谢。

this will work definately !!!这肯定会奏效!!!

  $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();
   }

I handle validation exceptions in Laravel 5.3 like this.我在 Laravel 5.3 中像这样处理验证异常。 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.如果您使用 Laravel Collective,它会自动在输入旁边显示错误,如果您使用 laracasts/flash,它还会显示第一个验证错误作为通知。


Handler.php render: Handler.php渲染:

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();
    }

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

Laravel 5:拉维尔5:

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

for back only:只回:

return back()->withInput();

You can try this:你可以试试这个:

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

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

It works for me.这个对我有用。

For Laravel 5对于 Laravel 5

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

For Laravel 6, 7 and 8对于 Laravel 6、7 和 8

return back()->withInput();

Docs :文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM