Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel中正则表达式规则的自定义验证消息?_Php_Validation_Laravel - Fatal编程技术网

Php Laravel中正则表达式规则的自定义验证消息?

Php Laravel中正则表达式规则的自定义验证消息?,php,validation,laravel,Php,Validation,Laravel,非常基本的问题,我试图在Laravel中自定义正则表达式验证规则的错误消息。具体规则是针对密码的,要求密码包含6-20个字符、至少一个数字和一个大小写字母,因此我希望将此信息传达给用户,而不仅仅是表示格式“无效”的默认消息 因此,我尝试以几种不同的方式将消息添加到lang文件中: (一) (二) (三) 这些都不起作用。有什么方法可以做到这一点吗?我可以用这个方法来解决这个问题: 'custom' => array( 'password' => array(

非常基本的问题,我试图在Laravel中自定义正则表达式验证规则的错误消息。具体规则是针对密码的,要求密码包含6-20个字符、至少一个数字和一个大小写字母,因此我希望将此信息传达给用户,而不仅仅是表示格式“无效”的默认消息

因此,我尝试以几种不同的方式将消息添加到lang文件中:

(一)

(二)

(三)


这些都不起作用。有什么方法可以做到这一点吗?

我可以用这个方法来解决这个问题:

'custom' => array(
    'password' => array(
        'regex' => 'Password must contain at least one number and both uppercase and lowercase letters.'
    )
)

但我很想知道,如果有人碰巧知道…,为什么其他方法中的一种不起作用?

看来laravel 7解决了这个问题:

        $messages = [
            'email.required' => 'We need to know your e-mail address!',
            'password.required' => 'How will you log in?',
            'password.confirmed' => 'Passwords must match...',
            'password.regex' => 'Regex!'
        ];
        $rules = [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => [
                'required',
                'string',
                'min:7',
                'confirmed',
                'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/'
            ]
        ];
        return Validator::make($data, $rules, $messages );
'custom' => array(
    'password.regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})' => 'Password must contain at least one number and both uppercase and lowercase letters.'
)
'custom' => array(
    'password' => array(
        'regex' => 'Password must contain at least one number and both uppercase and lowercase letters.'
    )
)
        $messages = [
            'email.required' => 'We need to know your e-mail address!',
            'password.required' => 'How will you log in?',
            'password.confirmed' => 'Passwords must match...',
            'password.regex' => 'Regex!'
        ];
        $rules = [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => [
                'required',
                'string',
                'min:7',
                'confirmed',
                'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/'
            ]
        ];
        return Validator::make($data, $rules, $messages );