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

5.8k views Asked by At

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

3

There are 3 answers

0
lluisaznar On BEST ANSWER

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

0
Laxman On

you could use callback validator.

use Zend\Validator\Callback;
$callback = new Callback([$this, 'validateFloat']);
$priceCallBack->setMessage('The value is not valid.');

then, somewhere in the class you need to have this function.

public function validateFloat($value){
return (is_numeric($value));
}

And finally inside the form, add this validator, for eg.

$this->inputFilter->add([
            'name'=>'pro_price',
            'required' => true,
            'validators'=>[$callback]
        ]);
0
Marcin Żurek On

You can write own validator like this:

class Currency extends \Zend\Validator\AbstractValidator {

    /**
     * Error constants
     */
    const ERROR_WRONG_CURRENCY_FORMAT = 'wrongCurrencyFormat';

    /**
     * @var array Message templates
     */
    protected $messageTemplates = array(
        self::ERROR_WRONG_CURRENCY_FORMAT => "Vaule is not valid currency format. (xxx | xxx.xx | xxx,xx)",
    );

    /**
     * {@inheritDoc}
     */
    public function isValid($value) {

        $exploded = null;
        if (strpos($value, '.')) {
            $exploded = explode('.', $value);
        }
        if (strpos($value, ',')) {
            $exploded = explode(',', $value);
        }

        if (!$exploded && ctype_digit($value)) {
            return true;
        }
        if (ctype_digit($exploded[0]) && ctype_digit($exploded[1]) && strlen($exploded[1]) == 2) {
            return true;
        }
        $this->error(self::ERROR_WRONG_CURRENCY_FORMAT);
        return false;
    }

}

And filter value:

class Float extends \Zend\Filter\AbstractFilter {

public function filter($value) {                    
    $float = $value;

    if($value){
        $float = str_replace(',', '.', $value);            
    }

    return $float;
}    

}