2015-05-22 60 views
1
  $inputFilter->add(array(
      'name' => 'nightCharges', 
      'required' => FALSE, 
      'filters' => array(
       array('name' => 'Digits'), 
      ), 
      'validators' => array(
       array(
        'name' => 'GreaterThan', 
        'options' => array(
         'min' => 1, 
         'messages' => array(
          \Zend\Validator\GreaterThan::NOT_GREATER => 'My message', 
         ), 
        ), 
       ), 
      ), 
     )); 

上面是我的代碼片段驗證在這種情況下'複選框'。如果我將'required'設置爲'true'並提交空表單,它會顯示默認的'Value is required and not empty'信息,如果我將'required'設置爲'false',它不會向我顯示任何信息錯誤。即使我提交非數字值,它也不會顯示實際的錯誤消息。 我在哪裏犯錯? 其實我正在研究'請接受條款&條件'複選框。zf2默認驗證消息不可覆蓋

回答

1

而不是使用GreaterThan驗證程序,請嘗試使用Identical以確保值不能是1以外的任何值。

'validators' => array(
    array(
     'name' => 'Identical', 
     'options' => array(
      'token' => '1', 
      'messages' => array(
       Identical::NOT_SAME => 'Please accept the terms & conditions.', 
      ), 
     ), 
    ), 
), 
+0

謝謝Svengali。但我的主要問題是因爲過濾器。一旦我刪除它們,代碼似乎工作正常,至少現在這些消息按照我的命令工作。將讓你知道,如果真的解決了我的問題。保重 – netmantle