duandu8202 2011-05-29 15:17 采纳率: 0%
浏览 46
已采纳

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

I have a 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?

  • 写回答

1条回答 默认 最新

  • doulilou8560 2011-05-29 15:25
    关注

    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:

    $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. You will need to know the error codes of the various validation error types for the particular validator

    Similar question here and here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统