如何以zend形式给出浮点数的验证器?

时间:2011-10-17 10:20:05

标签: php zend-framework zend-form

以下是从表单中获取的一段代码,

$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); 

当我尝试使用浮点数提交表单时,它会抛出我的错误“无效输入....”。它不验证浮点数。请建议。

2 个答案:

答案 0 :(得分:3)

使用Zend_Validate_Float

'validators' => array(
    array('Float', ...

答案 1 :(得分: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是可行的方法。