有没有办法注入错误消息,以便我们可以在重定向上使用$errors
实例在视图上显示它?
$errors->all(); // e.g. to have it here
我试过了:
return Redirect::to('dashboard')
->with('errors', 'Custom error');
但实际上会抛出错误:
Call to a member function all() on a non-object
答案 0 :(得分:4)
您的示例不起作用,因为您只传递变量而不是对象。
如果要将自己的自定义错误消息添加到其他验证错误,可以使用add()
类的Illuminate\Support\MessageBag
方法(因为验证错误将作为此类的实例返回):< / p>
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
$errors = $validator->messages();
$errors->add('Error', 'Custom Error');
return Redirect::to('dashboard')->withErrors($errors);
}
现在,您的自定义消息应与其他验证错误一起显示。
答案 1 :(得分:1)