PHP的-如何重定向回输入形式-Laravel 5
如果我的表单操作引发异常,如何使用给定的POST参数重定向回到我的表单页面?
8个解决方案
67 votes
您可以使用以下内容:
return Redirect::back()->withInput(Input::all());
如果您使用的是表单请求验证,这正是Laravel将错误和给定输入重定向回您的方式。
摘自return redirect()->to($this->getRedirectUrl())
->withInput($request->input())
->withErrors($errors, $this->errorBag());:
return redirect()->to($this->getRedirectUrl())
->withInput($request->input())
->withErrors($errors, $this->errorBag());
infomaniac answered 2020-02-18T06:25:09Z
39 votes
在您的字段值上写旧函数例如
Vishal Rambhiya answered 2020-02-18T06:25:28Z
14 votes
在您的HTML中,您必须使用$request->only()。如果不使用它,您将无法获取该值,因为会话将存储在其缓存中。
就像进行名称验证一样,这将是-
现在,如果重定向发生错误,则可以在提交后获取值。
return redirect()->back()->withInput();
正如@infomaniac所说,您也可以直接使用$request->only(),
return Redirect::back()->withInput(Input::all());
加:如果仅显示特定字段,请使用$request->only()
return redirect()->back()->withInput($request->only('name'));
希望,它可能在所有情况下都有效,谢谢。
Maniruzzaman Akash answered 2020-02-18T06:26:11Z
4 votes
我这样处理Laravel 5.3中的验证异常。 如果您使用Laravel Collective,它将在输入旁边自动显示错误;如果您使用laracast / flash,它还将显示第一个验证错误作为通知。
Handler.php呈现:
public function render($request, Exception $e)
{
if ($e instanceof \Illuminate\Validation\ValidationException) {
return $this->handleValidationException($request, $e);
}
(..)
}
和功能:
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();
}
Rav answered 2020-02-18T06:26:40Z
3 votes
Laravel 5:
return redirect(...)->withInput();
仅用于背部:
return back()->withInput();
Luca C. answered 2020-02-18T06:27:05Z
0 votes
这肯定会工作!
$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();
}
Aditya Tomar answered 2020-02-18T06:27:25Z
0 votes
$request->flash('request',$request);
这个对我有用。
Tuấn Anh Trần Văn answered 2020-02-18T06:27:45Z
0 votes
您可以尝试以下方法:
return redirect()->back()->withInput(Input::all())->with('message', 'Something
went wrong!');
RashedRahat answered 2020-02-18T06:28:05Z