zend form 元素上的 zend validate 问题
我曾经使用此表单元素来验证电子邮件并在格式无效时显示错误消息:
$email_users = new Zend_Form_Element_Text('email_users');
$email_users->setLabel('Email:')
->setRequired(false)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('EmailAddress')
->setErrorMessages(array('messages' => 'Invalid Email'));
setErrorMessages 工作正常,因为这是我需要的唯一验证,因此它用我的自定义消息替换了所有错误消息,现在我必须添加另一个验证以查看它是否已存在于我的数据库中:
$email_users = new Zend_Form_Element_Text('email_users');
$email_users->setLabel('Email:')
->setRequired(false)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidators(array(
array('EmailAddress', true,
array(
'messages' =>
array(Zend_Validate_EmailAddress::INVALID => 'Invalid Email')
)
),
array('Db_NoRecordExists', true,
array(
'messages' =>
array(Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND => 'Email already exists'),
'table' => 'users',
'field' => 'email_users')
)));
功能很好,问题是当电子邮件无效时,它现在向我显示默认的 zend 验证消息,但是当它存在时,它确实向我显示我的自定义消息。有没有办法以这种方式存档以前的功能? (替换所有无效的电子邮件)我无法使用 setErrorMessages,因为当电子邮件已存在时,这会显示“无效电子邮件”。
我尝试使用“消息”=> '错误'但没有任何反应(没有错误但显示默认消息),我尝试过:
$emailValidator = new Zend_Validate_EmailAddress(); $emailValidator->setMessages('无效的电子邮件');
在我的表单元素上,我添加了
$email_users->addValidator($emailValidator)
没有相同的结果。我得到的最接近的是做'messages' => array(Zend_Validate_EmailAddress::INVALID_FORMAT => 'Invalid email') 当我输入 'email@' 或 'email' 时,它会显示消息,但如果我输入 'email@host' 它会显示 3 个关于主机名、dns 的错误和本地网络,当我使用 setMessages('Error') 时它们不会显示(仅显示“错误”),
提前致谢。
I used to have this form element to validate an email and display an error message if the format was invalid:
$email_users = new Zend_Form_Element_Text('email_users');
$email_users->setLabel('Email:')
->setRequired(false)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('EmailAddress')
->setErrorMessages(array('messages' => 'Invalid Email'));
setErrorMessages worked fine because this was the only validation I needed so it replaced all error messages with my custom one, now I had to add another validation to see if it already existed in my DB:
$email_users = new Zend_Form_Element_Text('email_users');
$email_users->setLabel('Email:')
->setRequired(false)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidators(array(
array('EmailAddress', true,
array(
'messages' =>
array(Zend_Validate_EmailAddress::INVALID => 'Invalid Email')
)
),
array('Db_NoRecordExists', true,
array(
'messages' =>
array(Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND => 'Email already exists'),
'table' => 'users',
'field' => 'email_users')
)));
The functionality is fine, the problem is that when the email is invalid it now shows me the default zend validate messages, ut when it exists it does show me my custom message. Is there any way to archive the previous functionality this way? (Replacing all invalid email messages) I can't uset setErrorMessages since this shows me 'invalid email' when the email alrady exists.
I tried using 'messages' => 'Error' but nothing happens (no errors but the default messages show), I tried:
$emailValidator = new Zend_Validate_EmailAddress();
$emailValidator->setMessages('Invalid email');
And on my form element I added
$email_users->addValidator($emailValidator)
Nothing same results. The closest I have gotten is doing 'messages' => array(Zend_Validate_EmailAddress::INVALID_FORMAT => 'Invalid email') this shows the msg when I type something like 'email@' or 'email' but if I type 'email@host' it shows me 3 erros regarding the hostname, dns and localnetwork, which they don't show when I use setMessages('Error') (just displays 'Error'),
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发布了一个答案,解释了所有不同的错误消息设置函数如何在这里工作,
Zend 验证器和错误消息:addValidator 和 addErrorMessage
简而言之,试试这个:
'messages' => '电子邮件已存在'
而不是使用数组。
I posted an answer which explains how all the different error message setting functions work here,
Zend validators and error messages: addValidator and addErrorMessage
In short, try this:
'messages' => 'Email already exists'
instead of using an array.
您必须像这样编写验证器。
在所有无效电子邮件地址错误的情况下,都必须显示“电子邮件地址无效...!”。
You have to write validator like this..
In all cases of invalid email address error must show "Email Address is Not Valid... !".