ZF2 表单验证对电子邮件无法正常工作 [英] ZF2 form validation not working properly for email
问题描述
我有一个 type="email" 的输入框并使用 zend 验证器对其进行验证
i have a input box with type="email" and validation it with zend validator
<input type="email" name="email" > email </input>
'email' => array(
'required' => true,
'validators' => array(
array(
'name' => 'Regex',
'options' => array(
'pattern'=>'/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
'messages' => array(
\Zend\Validator\Regex::NOT_MATCH=>'Please fill correct email ',
)
),
'break_chain_on_failure' => true
),
),
问题是我是否使用另一个数组来检查
problem is if i use another array to check for
IS_EMPTY
zend regex 再次不显示上述错误,而是显示默认的 zend regex 错误,如果我删除 IS_EMPTY,则它可以正常工作.正则表达式错误是
zend regex again does not display the above error instead display default zend regex error and if i remove IS_EMPTY then it works fine. regex error is
1. input does not match to expression '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/'
完全无法理解为什么它在没有空检查的情况下可以正常工作并且不能与它一起工作
EXACTLY NOT ABLE TO UNDERSTAND WHY THIS WORKS FINE WITHOUT EMPTY CHECK AND DOESN'T WORK ALONG WITH IT
推荐答案
不需要使用notEmpty,只需要将字段设置为必填"并指定错误信息:
You don't need to use notEmpty, you only need to set the field as "required" and specify the error message:
$this->add(array(
'name' => 'email',
'required' => true,
'error_message' => 'Please entry e-mail.',
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array (
'messages' => array(EmailAddress::INVALID => 'Please specify a valid e-mail.'),
),
'break_chain_on_failure' => true,
),
),
));
这篇关于ZF2 表单验证对电子邮件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!