zend form 元素上的 zend validate 问题

发布于 2024-09-28 15:58:04 字数 2238 浏览 8 评论 0原文

我曾经使用此表单元素来验证电子邮件并在格式无效时显示错误消息:

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

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

发布评论

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

评论(2

你的笑 2024-10-05 15:58:04

我发布了一个答案,解释了所有不同的错误消息设置函数如何在这里工作,

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.

冬天旳寂寞 2024-10-05 15:58:04

您必须像这样编写验证器。

$email_users->addValidator(
    'EmailAddress', 
    true, 
    array( 'messages' => array( 'emailAddressInvalidFormat' => "Email Address is Not Valid... !<br>", "emailAddressInvalidHostname"=>"Email Address is Not Valid... !<br>", "hostnameUnknownTld"=>"Email Address is Not Valid... !<br>","hostnameLocalNameNotAllowed"=>"Email Address is Not Valid... !<br>") )
);

在所有无效电子邮件地址错误的情况下,都必须显示“电子邮件地址无效...!”

You have to write validator like this..

$email_users->addValidator(
    'EmailAddress', 
    true, 
    array( 'messages' => array( 'emailAddressInvalidFormat' => "Email Address is Not Valid... !<br>", "emailAddressInvalidHostname"=>"Email Address is Not Valid... !<br>", "hostnameUnknownTld"=>"Email Address is Not Valid... !<br>","hostnameLocalNameNotAllowed"=>"Email Address is Not Valid... !<br>") )
);

In all cases of invalid email address error must show "Email Address is Not Valid... !".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文