我在 Laravel 5.1 中需要有关如何命名 MessageBag 错误的帮助,以允许我检索特定表单的错误消息。
这可以在 Controller 的存储方法中实现,而无需使用请求。
return redirect('register')
->withErrors($validator, 'login');
但我使用的是 RegistrationRequest,所以我不需要验证我的 Controller 内的任何输入我只需要在请求中设置我的规则。
我尝试将这行代码添加到我的请求中
/**
* Format the errors from the given Validator instance.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return array
*/
protected function formatErrors(Validator $validator)
{
return $validator->errors()->getMessages('registration');
}
最佳答案
我有同样的问题,当我查看 FormRequest 类时,我发现有一个属性 protected $errorBag = 'default'
。
因此,通过在您的请求类中覆盖此属性,例如
protected $errorBag = 'login';
您可以确定该请求的错误范围,然后像这样在您的 View 中访问它们:
$errors->login->has('email')
关于php - Laravel 5.1 单页多表单命名消息包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248547/