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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31081644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:13:07  来源:igfitidea点击:

How to redirect back to form with input - Laravel 5

phpformslaravelredirectlaravel-5

提问by infomaniac

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

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

回答by infomaniac

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());
return redirect()->to($this->getRedirectUrl())
                    ->withInput($request->input())
                    ->withErrors($errors, $this->errorBag());

回答by Vishal Rambhiya

write old function on your fields value for example

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

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

回答by Maniruzzaman Akash

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.

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

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 @infomaniacsays, you can also use the Input classdirectly,

正如@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'));

Hope, it might work in all case, thanks.

希望,它可能适用于所有情况,谢谢。

回答by Aditya Tomar

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

回答by Rav

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.

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



Handler.phprender:

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

回答by Luca C.

Laravel 5:

拉维尔5:

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

for back only:

只回:

return back()->withInput();

回答by RashedRahat

You can use any of these two:

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

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

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

Or,

或者,

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

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

回答by Tu?n Anh Tr?n V?n

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

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

It works for me.

这个对我有用。

回答by RashedRahat

You can try this:

你可以试试这个:

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