Php 表单请求验证返回旧输入错误
我正在使用拉威尔的,我觉得非常有用,并使我的控制器更整洁 我的更复杂的验证片段,如果Php 表单请求验证返回旧输入错误,php,laravel,validation,laravel-5.4,Php,Laravel,Validation,Laravel 5.4,我正在使用拉威尔的,我觉得非常有用,并使我的控制器更整洁 我的更复杂的验证片段,如果验证程序失败,应该返回到上一页,使用用户的旧输入 有时,我还必须指定一个命名错误包,它似乎不是作为请求表单的选项内置的 不幸的是,这个额外的错误函数在默认情况下并没有随表单请求验证器一起出现 目前我有: $validator = Validator::make($request->all(), [ 'id' => 'required|numeric', 'name' => 're
验证程序失败,应该返回到上一页,使用用户的旧输入
有时,我还必须指定一个命名错误包,它似乎不是作为请求表单的选项内置的
不幸的是,这个额外的错误函数在默认情况下并没有随表单请求验证器一起出现
目前我有:
$validator = Validator::make($request->all(), [
'id' => 'required|numeric',
'name' => 'required|alpha_spaces',
'email' => 'required|email',
]);
if ($validator->fails()) {
return back()
->withErrors($validator, 'aNamedErrorBag')
->withInput();
}
如果验证程序失败,是否有方法将规则提取到请求验证程序文件中,并将输入返回到指定的错误包aNamedErrorBag
非常感谢FormRequest
类将错误包的名称作为属性:
/**
* The key to be used for the view error bag.
*
* @var string
*/
protected $errorBag = 'default';
您可以将此值更改为希望将错误发送到的errorBag。只需将其作为属性添加到自定义请求类中即可
编辑
这是FormRequest在验证错误时返回的结果:
/**
* Get the proper failed validation response for the request.
*
* @param array $errors
* @return \Symfony\Component\HttpFoundation\Response
*/
public function response(array $errors)
{
if ($this->expectsJson()) {
return new JsonResponse($errors, 422);
}
return $this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}
FormRequest
类将错误包的名称变为属性:
/**
* The key to be used for the view error bag.
*
* @var string
*/
protected $errorBag = 'default';
您可以将此值更改为希望将错误发送到的errorBag。只需将其作为属性添加到自定义请求类中即可
编辑
这是FormRequest在验证错误时返回的结果:
/**
* Get the proper failed validation response for the request.
*
* @param array $errors
* @return \Symfony\Component\HttpFoundation\Response
*/
public function response(array $errors)
{
if ($this->expectsJson()) {
return new JsonResponse($errors, 422);
}
return $this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}
杰出的谢谢你,为什么。默认情况下,它会返回用户的旧输入吗?@Ben-Yup。我添加了一个函数,可以将错误时的FormRequest验证数据返回到答案。甚至更好。再次感谢上帝:)太好了!谢谢你,为什么。默认情况下,它会返回用户的旧输入吗?@Ben-Yup。我添加了一个函数,可以将错误时的FormRequest验证数据返回到答案。甚至更好。再次感谢上帝:)