dpziir0079 2012-03-02 13:56
浏览 25
已采纳

当0为0时,Zend Form Element Regex Validator似乎拒绝了值

Today, I have come across a bug in my Zend Framework application. This is the case:

I have a form with an element that has a regex validator on it. When testing the form, I found out that I can not provide the value 0 for the element. Zend gives me an error, telling me that I have not filled in the element.

The element:

$element = $this->createElement( "text", "amount", array( "label" => "Amount", "required" => true ) );
$element->addValidator( 'regex', false, array( '/^[-]?([0-9]+)(([,.][0-9]+)+)?$/' ) );

Can anyone tell me what the reason might be that Zend tells me I have not filled in the element when the submitted value is 0?

Thx

  • 写回答

1条回答 默认 最新

  • douji8347 2012-03-02 14:19
    关注

    It's very simple, when you set "required" as true, Zend_Form_Element automatically adds a notEmpty() validator at the top of the validators stack.

    Therefore, any null, 0, "", etc. values are considered false, and this validator set an error to your form.

    See the manual for more information:

    Default behaviour for Zend_Validate_NotEmpty

    By default, this validator works differently than you would expect when you've worked with PHP's empty() function. In particular, this validator will evaluate both the integer 0 and string '0' as empty.

    $valid = new Zend_Validate_NotEmpty();
    $value  = '';
    $result = $valid->isValid($value);
    // returns false
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂