Laravel中的正则表达式规则的自定义验证消息? [英] Custom validation message for regex rule in Laravel?

查看:274
本文介绍了Laravel中的正则表达式规则的自定义验证消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常基本的问题,我正在尝试为Laravel中的正则表达式验证规则自定义错误消息.该特定规则适用于密码,并且要求密码包含6-20个字符,至少一个数字以及大写和小写字母,因此我想将此信息传达给用户,而不仅仅是默认消息,该消息说格式为无效".

Pretty basic question, I'm trying to customise the error message for a regex validation rule in Laravel. The particular rule is for passwords and requires the password to have 6-20 characters, at least one number and an uppercase and lowercase letter so I'd like to communicate this to the user rather than just the default message which says the format is "invalid".

所以我尝试通过几种不同的方式将消息添加到lang文件中:

So I tried to add the message into the lang file in a few different ways:

1)

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

2)

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

3)

'custom' => array(
    'password.regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})' => 'Password must contain at least one number and both uppercase and lowercase letters.'
)

这些都不起作用.有办法吗?

None of these have worked. Is there a way to do this?

推荐答案

我能够改用以下方法解决此问题:

I was able to solve this by using this method instead:

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

但是我很想知道,如果有人碰巧知道...,为什么其他方法之一不起作用??

but I'd love to know why one of the other methods didn't work if anyone happens to know...?

这篇关于Laravel中的正则表达式规则的自定义验证消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆