如何以 zend 形式提供浮点数验证器?

发布于 2024-12-10 00:07:13 字数 1327 浏览 0 评论 0原文

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

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

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

Following is the piece of code which has been taken from 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); 

When i try submit the form using floating number , it throws my error "Invalid Enter....". It does not validate floating point number. Kindly advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2024-12-17 00:07:13

使用 Zend_Validate_Float

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

Use Zend_Validate_Float:

'validators' => array(
    array('Float', ...
我的痛♀有谁懂 2024-12-17 00:07:13

您收到错误消息是因为 Digits 验证器仅验证数字。 Zend_Validate 文档

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 是正确的方法。

You are getting the error message because the Digits validator only validates digits. There is a comment about it in the 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. 

As @Zyava says, using Zend_Validate_Float is the way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文