mymotherland
mymotherland

Reputation: 8228

How to give validator for floating point number in zend form?

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.

Upvotes: 1

Views: 4441

Answers (2)

dinopmi
dinopmi

Reputation: 2673

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.

Upvotes: 1

Dmytro Zavalkin
Dmytro Zavalkin

Reputation: 5277

Use Zend_Validate_Float:

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

Upvotes: 3

Related Questions