2011-10-17 36 views
1

以下是一段代碼已經從形式拍攝,如何在Zend表單中爲浮點數賦予驗證器?

$debit_amount = new Zend_Form_Element_Text('debit_amount', array(
        'label' => 'Debit_amount', 
        'value' => '', 
        'class' => 'text-size text', 
        'required' => true, 
        'tabindex' => '13', 
        'validators' => array(
         array('Digits', false, array(
           'messages' => array(
            'notDigits' => "Invalid entry, ex. 10.00", 
            'digitsStringEmpty' => "", 
          ))), 
         array('notEmpty', true, array(
           'messages' => array(
            'isEmpty' => 'Debit_amount can\'t be empty' 
           ) 
         )), 

        ), 
        'filters' => array('StringTrim'), 
        //'decorators' => $this->requiredElementDecorators, 
        //'description' => '<img src="' . $baseurl . '/images/star.png" alt="required" />', 
       )); 
     $this->addElement($debit_amount); 

當我嘗試使用浮點數提交表單,它拋出我的錯誤「無效的輸入......」。它不驗證浮點數。友善的建議。

回答

1

你所得到的錯誤消息,因爲Digits驗證只驗證數字。有關於它的Zend_Validate documentation評論:

Note: Validating numbers 
When you want to validate numbers or numeric values, be aware that this validator only 
validates digits. This means that any other sign like a thousand separator or a comma 
will not pass this validator. In this case you should use Zend_Validate_Int or 
Zend_Validate_Float. 

由於@Zyava說,使用Zend_Validate_Float是要走的路。