简体   繁体   English

Zend form_element 添加带有自定义错误消息的验证器

[英]Zend form_element add validator with custom error message

I have a Zend_Form_element:我有一个 Zend_Form_element:

$text=new Zend_Form_Element_Text('text');

I added a regex validator to it:我添加了一个正则表达式验证器:

$text->addValidator('regex', false, array('/[\\?\\&]v=([^\\?\\&]+)/'));

How could I set a customized error message for the validator?如何为验证器设置自定义错误消息?

You can add custom validation error messages if you know what specific error codes the validator provides.如果您知道验证器提供的特定错误代码,则可以添加自定义验证错误消息。 In case of regex, I believe it gives an "regexNotMatch" error, so for this particular case, you could use:在正则表达式的情况下,我相信它会给出“regexNotMatch”错误,因此对于这种特殊情况,您可以使用:

$text->addValidator('regex', false, array(
    '/[\\?\\&]v=([^\\?\\&]+)/',
    'messages'=>array(
    'regexNotMatch'=>'There was some random custom error'
    )    
));

For more information, have a look here .欲了解更多信息,请查看此处

Some developers may wish to provide custom error messages for a validator.一些开发人员可能希望为验证器提供自定义错误消息。 The $options argument of the Zend_Form_Element::addValidator() method allows you to do so by providing the key 'messages' and mapping it to an array of key/value pairs for setting the message templates. Zend_Form_Element::addValidator() 方法的 $options 参数允许您通过提供键 'messages' 并将其映射到键/值对数组来设置消息模板。 You will need to know the error codes of the various validation error types for the particular validator您需要知道特定验证器的各种验证错误类型的错误代码

Similar question here and here类似的问题在这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM