phpzend-framework2zend-form2zend-inputfilter

How to validate if an input has a float value in zf2 form?


I have this in my class

$inputFilter->add(
                    $factory->createInput(array(
                        'name' => 'precio',
                        'required' => true,
                        'validators' => array(
                            array(
                                'name' => 'Float',
                                'options' => array(
                                    'min' => 0,
                                ),
                            ),
                        ),
                    ))
            );

When I enter a Integer number like 5 or 78 everything seems ok, but when I try with a number like 5.2 I got the next error message

The input does not appear to be a float


Solution

  • The decimal character in the Float Validator class depends on the locale used in the application. Try adding the locale as an option like this:

    $factory->createInput( array(
        'name' => 'precio',
        'required' => true,
        'validators' => array(
            array(
                'name' => 'Float',
                'options' => array(
                    'min' => 0,
                    'locale' => '<my_locale>'
                ),
            ),
        ),
    ) );
    

    If you don't set the locale, the Float class gets the intl.default_locale defined in php.ini