消息在 Zend_Form_Element_Text 上不起作用
在表单中,我有以下元素:
$email = new Zend_Form_Element_Text('username');
$email
->setLabel($this->getView()->l('E-mail'))
->setRequired(TRUE)
->addValidator('EmailAddress')
->addValidator('Db_NoRecordExists', true,
array(
'table' => 'pf_user',
'field' => 'email',
'messages' => array(
'recordFound' => 'This username is already registered',
)
))
->setErrorMessages(array(
'emailAddressInvalidFormat' => 'You must enter a valid e-mail',
'isEmpty' => 'You must enter an e-mail',
'recordFound' => 'This e-mail has already registered in out database'
));
$form->addElement($email)
问题是我总是收到相同的消息“您必须输入有效的电子邮件”(第一个)。有谁知道错误是什么??
In a form, I have the following element:
$email = new Zend_Form_Element_Text('username');
$email
->setLabel($this->getView()->l('E-mail'))
->setRequired(TRUE)
->addValidator('EmailAddress')
->addValidator('Db_NoRecordExists', true,
array(
'table' => 'pf_user',
'field' => 'email',
'messages' => array(
'recordFound' => 'This username is already registered',
)
))
->setErrorMessages(array(
'emailAddressInvalidFormat' => 'You must enter a valid e-mail',
'isEmpty' => 'You must enter an e-mail',
'recordFound' => 'This e-mail has already registered in out database'
));
$form->addElement($email)
the problem is that I always I get the same message "You must enter a valid e-mail" (the first one). Does anybody knows what is the mistake??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您正在做的事情如下:
“您必须输入有效的电子邮件”
更正确的方法是在验证器中设置自定义消息。当调用验证器来验证元素时,如果验证失败,验证器将调用元素上的 setErrorMessages 来设置您指定的自定义错误。使用下面的此类代码来设置您的自定义消息。
您会发现每个验证器类中通常都有常量指定一种错误类型。在本例中,常量位于 DB_NoRecordExists 类的父类中,但通常您会直接在顶部附近的类中找到它们。
Actually, what you're doing is the following :
"You must enter a valid e-mail"
The more correct way is to set the custom messages in the validator. When the validators are called to validate the element, if the validation fails, the validator will call the setErrorMessages on the element to set the custom errors you specify. Use this type of code below to set your custom messages.
You'll find that usually there are consts in each validator class that specify one type of error. In this case, the consts are in the parent class of the DB_NoRecordExists class but usually you'll find them directly in the class near the top.
基本上,通过将 'true' 作为第二个参数传递给
addValidator()
,您可以说验证器在验证器失败时打破链条。由于“”不是有效的电子邮件地址,因此第一个电子邮件验证器失败并破坏了链来自 Zend Doc http://framework.zend.com/manual/en/zend.validate.validator_chains.html
Basically by passing 'true' as second parameter to
addValidator()
you are saying the validator to break the chain whenever validator fails . Since "" is not an valid email address hence the first email validator fails and breaks the chainFrom Zend Doc http://framework.zend.com/manual/en/zend.validate.validator_chains.html