laravel中的自定义验证错误

时间:2015-10-08 11:57:01

标签: php laravel

在更新配置文件时,我使用验证器类方法:

class UpdateRequest extends Request {
    public function authorize() { return true; }
    public function rules() {
        return [
            'name' => 'required',
            'email' => 'required|email',
        ];
    }
}

如何添加其他验证错误,如:

public function postUpdate(UpdateRequest $request)
    if($user->email == $request->get('email')) {
        $request->addEerror("The email has already been taken."); //shows an fatal error        
    }
}

谢谢

1 个答案:

答案 0 :(得分:1)

假设5.1

,您没有提到您正在使用的Laravel版本

您可以为不同的验证类型创建消息数组,如下例所示:

$rules = [
     'first_name' => 'required',
     'last_name' => 'required',
     'email' => 'required|email|unique:users,email,'.$user->id
];

在您的resources / lang / en / validation.php文件中

$custom_messages = [
     'required' => 'The :attribute field is required.',
     'email' => [
         'required' => 'The email id field is required.',
         'email' => 'Please enter a valid email format.',
         'unique' => 'The email id has already been taken.',
     ]
];

这应该可以解决问题。