I've got an Input Filter whose validator config for an email field looks like;
我有一个输入过滤器,它的验证器配置为一个电子邮件字段;
'validators' => array(
array (
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
'emailAddressInvalidFormat' => "Email address doesn't appear to be valid.",
)
),
),
array (
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
'isEmpty' => 'Email address is required',
)
),
),
),
),
It works, that part is fine, but what I will get forever laughed at by the business units here, is if I put out an app that spits this error message to users:
这是可行的,这部分没问题,但我将永远被这里的商业单位嘲笑,如果我发布一个应用程序,向用户发布这个错误信息:
The input does not match against pattern
输入与模式不匹配
'/^[a-zA-Z0-9.!#$%&'+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)$/'
' / ^[a-zA-Z0-9 ! # $ % & ' + / = ? ^ _ { | } ~ -)+ @(a-zA-Z0-9 -)+(?:[a-zA-Z0-9 -]+)$ / '
There's a strange nerd comedy buried in there (yes I realize it's accurate, but, rofl).
里面隐藏着一个奇怪的书呆子喜剧(是的,我知道它是准确的,但是,rofl)。
I have two questions for the kind souls here:
我有两个问题要问善良的灵魂
How can I customize that error message? I can't seem to find the right key as I easily had for 'emailAddressInvalidFormat'
.
如何定制错误消息?我似乎找不到正确的钥匙,因为我很容易找到“emailAddressInvalidFormat”。
Also, Is it possible to roll all the errors up into one? What I mean by that is. Rather than posting:
而且,是否有可能把所有的错误都卷成一个?我的意思是。而不是发布:
"Your email pattern just left the building & Your email cannot be blank & Your email doesn't appear to be valid"
“你的电子邮件模式刚刚离开大楼&你的电子邮件不能为空&你的电子邮件似乎不有效”
Can I put a "single failure" message for email?
我可以在电子邮件中加入“单一失败”信息吗?
"Hey bud, check your email, something ain't right!"
“嘿,巴德,检查你的邮件,有些不对劲!”
Thanks for your help as always.
一如既往地感谢您的帮助。
UPDATE
更新
Vote for this bug here https://github.com/zendframework/zend-validator/issues/41
请在这里为这个bug投票,https://github.com/zendframework/zend validator/es/ 41。
4 个解决方案
#1
4
Try this for custom message for email validation in ZF2:
在ZF2中,为电子邮件验证定制消息,请尝试以下操作:
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
\Zend\Validator\EmailAddress::INVALID_FORMAT => '"Hey bud, check your email, something ain\'t right!"'
)
)
)
)
#2
2
According to ZF2, Email address validations are:
根据ZF2,电子邮件地址验证是:
$this->add(array(
'name' => 'email',
'required' => true,
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'message' => array(
\Zend\Validator\EmailAddress::INVALID => "Invalid type given. String expected",
\Zend\Validator\EmailAddress::INVALID_FORMAT =>"The input is not a valid email address. Use the basic format local-part@hostname",
\Zend\Validator\EmailAddress::INVALID_HOSTNAME =>"'%hostname%' is not a valid hostname for the email address",
\Zend\Validator\EmailAddress::INVALID_MX_RECORD =>"'%hostname%' does not appear to have any valid MX or A records for the email address" ,
\Zend\Validator\EmailAddress::INVALID_SEGMENT =>"'%hostname%' is not in a routable network segment. The email address should not be resolved from public network",
\Zend\Validator\EmailAddress::DOT_ATOM =>"'%localPart%' can not be matched against dot-atom format" ,
\Zend\Validator\EmailAddress::QUOTED_STRING =>"'%localPart%' can not be matched against quoted-string format",
\Zend\Validator\EmailAddress::INVALID_LOCAL_PART =>"'%localPart%' is not a valid local part for the email address" ,
\Zend\Validator\EmailAddress::LENGTH_EXCEEDED =>"The input exceeds the allowed length",
),
),
),
),
));
And additional validation could be Regex as stated above, and I also have NoObjectExists to make sure the email is not in my db
,if the email address is used to login:
如前所述,还可以使用Regex进行额外的验证,如果使用电子邮件地址登录,我也有NoObjectExists以确保电子邮件不在我的db中:
array(
'name' => 'DoctrineModule\Validator\NoObjectExists',
'options' => array(
'object_repository' => $sm->get('doctrine.entitymanager.orm_default')->getRepository('MyMudole\Entity\User'),
'fields' => 'email',
'message'=>'This email is associated with another user! Please use another email',
),
),
#3
0
“The input does not match against pattern” message seems to be Zend\Validator\Regex::NOT_MATCH
actually, not of Zend\Validator\EmailAddress
class.
“输入与模式不匹配”消息似乎是Zend\Validator\Regex::NOT_MATCH,而不是Zend\Validator\EmailAddress类。
From your code, it is not clear where and whether you make use of the Regex
validator. I believe EmailAddress
does not use Regex
internally.
从您的代码中,不清楚您在哪里以及是否使用了Regex验证器。我认为EmailAddress在内部不使用Regex。
If you would like to customize the Regex
message, it would probably look like this:
如果您想要自定义Regex消息,可能会如下所示:
array (
'name' => 'Regex',
'options' => array(
'messages' => array(
'regexNotMatch' => "Not so nerdy message here.",
)
),
),
#4
0
It's not possible to especify just one message, but it should work as well:
我们不可能只将一条信息具体化,但它也应该发挥作用:
$message = sprintf(
$this->getTranslate()->translate(
'%s filled is not a valid e-mail address, please inform a valid in the field.'
),
$entity
);
return array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
\Zend\Validator\EmailAddress::INVALID => $message,
\Zend\Validator\EmailAddress::INVALID_FORMAT => $message,
\Zend\Validator\EmailAddress::INVALID_HOSTNAME => $message,
\Zend\Validator\EmailAddress::INVALID_MX_RECORD => $message,
\Zend\Validator\EmailAddress::INVALID_SEGMENT => $message,
\Zend\Validator\EmailAddress::DOT_ATOM => $message,
\Zend\Validator\EmailAddress::QUOTED_STRING => $message,
\Zend\Validator\EmailAddress::INVALID_LOCAL_PART => $message,
\Zend\Validator\EmailAddress::LENGTH_EXCEEDED => $message,
),
),
'break_chain_on_failure' => true
);
#1
4
Try this for custom message for email validation in ZF2:
在ZF2中,为电子邮件验证定制消息,请尝试以下操作:
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
\Zend\Validator\EmailAddress::INVALID_FORMAT => '"Hey bud, check your email, something ain\'t right!"'
)
)
)
)
#2
2
According to ZF2, Email address validations are:
根据ZF2,电子邮件地址验证是:
$this->add(array(
'name' => 'email',
'required' => true,
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'message' => array(
\Zend\Validator\EmailAddress::INVALID => "Invalid type given. String expected",
\Zend\Validator\EmailAddress::INVALID_FORMAT =>"The input is not a valid email address. Use the basic format local-part@hostname",
\Zend\Validator\EmailAddress::INVALID_HOSTNAME =>"'%hostname%' is not a valid hostname for the email address",
\Zend\Validator\EmailAddress::INVALID_MX_RECORD =>"'%hostname%' does not appear to have any valid MX or A records for the email address" ,
\Zend\Validator\EmailAddress::INVALID_SEGMENT =>"'%hostname%' is not in a routable network segment. The email address should not be resolved from public network",
\Zend\Validator\EmailAddress::DOT_ATOM =>"'%localPart%' can not be matched against dot-atom format" ,
\Zend\Validator\EmailAddress::QUOTED_STRING =>"'%localPart%' can not be matched against quoted-string format",
\Zend\Validator\EmailAddress::INVALID_LOCAL_PART =>"'%localPart%' is not a valid local part for the email address" ,
\Zend\Validator\EmailAddress::LENGTH_EXCEEDED =>"The input exceeds the allowed length",
),
),
),
),
));
And additional validation could be Regex as stated above, and I also have NoObjectExists to make sure the email is not in my db
,if the email address is used to login:
如前所述,还可以使用Regex进行额外的验证,如果使用电子邮件地址登录,我也有NoObjectExists以确保电子邮件不在我的db中:
array(
'name' => 'DoctrineModule\Validator\NoObjectExists',
'options' => array(
'object_repository' => $sm->get('doctrine.entitymanager.orm_default')->getRepository('MyMudole\Entity\User'),
'fields' => 'email',
'message'=>'This email is associated with another user! Please use another email',
),
),
#3
0
“The input does not match against pattern” message seems to be Zend\Validator\Regex::NOT_MATCH
actually, not of Zend\Validator\EmailAddress
class.
“输入与模式不匹配”消息似乎是Zend\Validator\Regex::NOT_MATCH,而不是Zend\Validator\EmailAddress类。
From your code, it is not clear where and whether you make use of the Regex
validator. I believe EmailAddress
does not use Regex
internally.
从您的代码中,不清楚您在哪里以及是否使用了Regex验证器。我认为EmailAddress在内部不使用Regex。
If you would like to customize the Regex
message, it would probably look like this:
如果您想要自定义Regex消息,可能会如下所示:
array (
'name' => 'Regex',
'options' => array(
'messages' => array(
'regexNotMatch' => "Not so nerdy message here.",
)
),
),
#4
0
It's not possible to especify just one message, but it should work as well:
我们不可能只将一条信息具体化,但它也应该发挥作用:
$message = sprintf(
$this->getTranslate()->translate(
'%s filled is not a valid e-mail address, please inform a valid in the field.'
),
$entity
);
return array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
\Zend\Validator\EmailAddress::INVALID => $message,
\Zend\Validator\EmailAddress::INVALID_FORMAT => $message,
\Zend\Validator\EmailAddress::INVALID_HOSTNAME => $message,
\Zend\Validator\EmailAddress::INVALID_MX_RECORD => $message,
\Zend\Validator\EmailAddress::INVALID_SEGMENT => $message,
\Zend\Validator\EmailAddress::DOT_ATOM => $message,
\Zend\Validator\EmailAddress::QUOTED_STRING => $message,
\Zend\Validator\EmailAddress::INVALID_LOCAL_PART => $message,
\Zend\Validator\EmailAddress::LENGTH_EXCEEDED => $message,
),
),
'break_chain_on_failure' => true
);