phpzend-frameworkzend-formzend-form-elementzend-validate

Zend Form can't validate float number


I have this form element:

$this->addElement('text', 'prezzo', array(
        'label' => 'Prezzo (*)',
        'filter' => '',
        'description' => 'Il prezzo non è comprensivo di sconto, ma se previsto verrà calcolato',
        'required' => true,
        'validators' => array('Float'),
        'placeholder' => 'Prezzo Pneumatico',
        'class' => 'form-control'
    ));

if i put in input a number like this 24.50 I get error on validation while if i put a number like this 24,50 I don't get any error.

I think the problem is with Zend_Locale in my Bootstrap.php where i set this value:

protected function _initLocale() {
    $locale = new Zend_Locale ( 'it' );
    Zend_Registry::set ( 'locale', $locale );
}

Probably one soluction is to filter the input and replace '.' with ','. Can you help me?


Solution

  • If you want validate in en locale e.g. 24.50 set locale option to your validator.

    $this->addElement('text', 'prezzo', array(
        'label' => 'Prezzo (*)',
        'filter' => '',
        'description' => 'Il prezzo non è comprensivo di sconto, ma se previsto verrà calcolato',
        'required' => true,
        'validators' => array(array('Float', true, array('locale' => 'en'))),
        'placeholder' => 'Prezzo Pneumatico',
        'class' => 'form-control'
    ));