消息在 Zend_Form_Element_Text 上不起作用

发布于 2024-12-04 13:58:59 字数 1133 浏览 0 评论 0原文

在表单中,我有以下元素:

    $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-12-11 13:58:59

实际上,您正在做的事情如下:

  1. 您在元素上设置了错误
  2. Zend 现在认为该元素没有正确验证并且第一个错误是
    “您必须输入有效的电子邮件”
  3. 当您显示表单时,由于您设置了错误,Zend 将找到它们并显示它找到的第一个错误。如果你改变顺序,你会发现你放在最上面的错误就是你得到的错误。

更正确的方法是在验证器中设置自定义消息。当调用验证器来验证元素时,如果验证失败,验证器将调用元素上的 setErrorMessages 来设置您指定的自定义错误。使用下面的此类代码来设置您的自定义消息。

$element->addValidator( array( 'Db_NoRecordExists', true, array( 
'messages' = array(
Zend_Validate_Db_Abstract::ERROR_NO_RECORD_FOUND => 'Myy custom no error record',
Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND    => 'My custom record error'
)
) ) );

您会发现每个验证器类中通常都有常量指定一种错误类型。在本例中,常量位于 DB_NoRecordExists 类的父类中,但通常您会直接在顶部附近的类中找到它们。

Actually, what you're doing is the following :

  1. You set the errors on the element
  2. Zend now thinks that the element did not validate correctly and that the first error is
    "You must enter a valid e-mail"
  3. When you display the form, since you set errors, Zend will find them and display the first one it finds. If you switch the order then you'll find that whichever error you put up top will be the error you get.

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.

$element->addValidator( array( 'Db_NoRecordExists', true, array( 
'messages' = array(
Zend_Validate_Db_Abstract::ERROR_NO_RECORD_FOUND => 'Myy custom no error record',
Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND    => 'My custom record error'
)
) ) );

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.

演出会有结束 2024-12-11 13:58:59

基本上,通过将 'true' 作为第二个参数传递给 addValidator() ,您可以说验证器在验证器失败时打破链条。由于“”不是有效的电子邮件地址,因此第一个电子邮件验证器失败并破坏了链

来自 Zend Doc http://framework.zend.com/manual/en/zend.validate.validator_chains.html

在某些情况下,让验证者打破链条是有意义的,如果
它的验证过程失败。 Zend_Validate 支持此类用例
将第二个参数传递给 addValidator() 方法。通过设置
$breakChainOnFailure 为 TRUE,添加的验证器将破坏链
失败时执行,避免运行任何其他验证
被确定为不必要或不适当的
情况。如果上面的例子写成如下,那么
如果字符串长度,则不会进行字母数字验证
验证失败:

$validatorChain->addValidator(
                    new Zend_Validate_StringLength(array('min' => 6,
                                                         'max' => 12)),
                    true)
               ->addValidator(new Zend_Validate_Alnum());

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 chain

From Zend Doc http://framework.zend.com/manual/en/zend.validate.validator_chains.html

In some cases it makes sense to have a validator break the chain if
its validation process fails. Zend_Validate supports such use cases
with the second parameter to the addValidator() method. By setting
$breakChainOnFailure to TRUE, the added validator will break the chain
execution upon failure, which avoids running any other validations
that are determined to be unnecessary or inappropriate for the
situation. If the above example were written as follows, then the
alphanumeric validation would not occur if the string length
validation fails:

$validatorChain->addValidator(
                    new Zend_Validate_StringLength(array('min' => 6,
                                                         'max' => 12)),
                    true)
               ->addValidator(new Zend_Validate_Alnum());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文