【问题标题】:Laravel 5 Custom validation rule message errorLaravel 5 自定义验证规则消息错误
【发布时间】:2015-09-07 09:23:13
【问题描述】:

想检查我是否已经制作了一个 CustomValidator.php 来处理我所有的附加验证规则,但问题是我应该如何返回自定义错误消息?这就是我为我的 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'
    ];
}

注意到验证部分正在工作,只是它不会传递自定义错误消息,它返回给我一个错误

CustomValidator.php line 18:
Undefined offset: 0

位于$parameter[0] 部分

【问题讨论】:

    标签: laravel laravel-5 laravel-validation


    【解决方案1】:

    找到了解决方案,显然当您尝试执行验证时,它出现的错误消息将携带该验证规则错误消息的关键。我们以下图为例,

    请注意,在电子邮件字段下,有一个错误消息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'
        ];
    }
    

    这将是下图中的最终结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2016-10-21
      • 2021-12-09
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 2015-01-26
      相关资源
      最近更新 更多