Laravel 5 自定义验证规则消息错误
Laravel 5 Custom validation rule message error
想检查一下我是否已创建 CustomValidator.php 来处理我所有的附加验证规则,但问题是我应该如何 return 自定义错误消息?这就是我为 CustomValidator.php 文件所做的,
<?php namespace App\Validators\CustomValidator;
use Illuminate\Validation\Validator;
use Auth;
class CustomValidator extends Validator
{
public function validateVerifyPassword($attribute, $value, $parameters)
{
$currentUser = Auth::user();
$credentials = array ('email' => $currentUser->email, 'password' => $currentUser->password);
return Auth::validate($credentials);
}
protected function replaceVerifyPassword($message, $attribute, $rule, $parameters)
{
return str_replace($attribute, $parameters[0], $message);
}
}
这就是我在 FormRequest.php
中定义自定义错误消息的方式
public function messages()
{
return [
'login_email.required' => 'Email cannot be blank',
'old_password.required' => 'You need to provide your current password',
'old_password.between' => 'Your current password must be between :min and :max characters',
'old_password.verifyPassword' => 'Invalid password',
'password.required' => 'Password is required.',
'password.between' => 'Your password must be between :min and :max characters',
'password_confirmation.required' => 'You need to retype your password',
'password_confirmation.same' => 'Your new password input do not match',
'g-recaptcha-response.required' => 'Are you a robot?',
'g-recaptcha-response.captcha' => 'Captcha session timeout'
];
}
注意到验证部分正在运行,只是它不会传递自定义错误消息,它 return 我的错误是
CustomValidator.php line 18:
Undefined offset: 0
在$parameter[0]
部分
找到了解决办法,显然当你尝试执行验证时,它出现的错误消息将携带该验证规则错误消息的关键。我们以下图为例,
请注意,在电子邮件字段下,有一条错误消息 validation.current_email
错误,current_email
是用于在 FormRequest 中指定您的自定义错误消息的键。所以基本上你所做的是在我的 FormRequest.php 中,我添加了这样的错误消息:
public function messages()
{
return [
'new_email.required' => 'New email cannot be blank',
'new_email.current_email' => 'This is the current email adderess being used',
'password.required' => 'You need to provide your current password',
'password.between' => 'Your current password must be between :min and :max characters',
'password.verify_password' => 'Invalid password',
'g-recaptcha-response.required' => 'Are you a robot?',
'g-recaptcha-response.captcha' => 'Captcha session timeout'
];
}
这将是下图中的最终结果:
想检查一下我是否已创建 CustomValidator.php 来处理我所有的附加验证规则,但问题是我应该如何 return 自定义错误消息?这就是我为 CustomValidator.php 文件所做的,
<?php namespace App\Validators\CustomValidator;
use Illuminate\Validation\Validator;
use Auth;
class CustomValidator extends Validator
{
public function validateVerifyPassword($attribute, $value, $parameters)
{
$currentUser = Auth::user();
$credentials = array ('email' => $currentUser->email, 'password' => $currentUser->password);
return Auth::validate($credentials);
}
protected function replaceVerifyPassword($message, $attribute, $rule, $parameters)
{
return str_replace($attribute, $parameters[0], $message);
}
}
这就是我在 FormRequest.php
中定义自定义错误消息的方式public function messages()
{
return [
'login_email.required' => 'Email cannot be blank',
'old_password.required' => 'You need to provide your current password',
'old_password.between' => 'Your current password must be between :min and :max characters',
'old_password.verifyPassword' => 'Invalid password',
'password.required' => 'Password is required.',
'password.between' => 'Your password must be between :min and :max characters',
'password_confirmation.required' => 'You need to retype your password',
'password_confirmation.same' => 'Your new password input do not match',
'g-recaptcha-response.required' => 'Are you a robot?',
'g-recaptcha-response.captcha' => 'Captcha session timeout'
];
}
注意到验证部分正在运行,只是它不会传递自定义错误消息,它 return 我的错误是
CustomValidator.php line 18:
Undefined offset: 0
在$parameter[0]
部分
找到了解决办法,显然当你尝试执行验证时,它出现的错误消息将携带该验证规则错误消息的关键。我们以下图为例,
请注意,在电子邮件字段下,有一条错误消息 validation.current_email
错误,current_email
是用于在 FormRequest 中指定您的自定义错误消息的键。所以基本上你所做的是在我的 FormRequest.php 中,我添加了这样的错误消息:
public function messages()
{
return [
'new_email.required' => 'New email cannot be blank',
'new_email.current_email' => 'This is the current email adderess being used',
'password.required' => 'You need to provide your current password',
'password.between' => 'Your current password must be between :min and :max characters',
'password.verify_password' => 'Invalid password',
'g-recaptcha-response.required' => 'Are you a robot?',
'g-recaptcha-response.captcha' => 'Captcha session timeout'
];
}
这将是下图中的最终结果: