Zend Form can't validate float number

1.2k views Asked by At

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?

2

There are 2 answers

2
venca On BEST ANSWER

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'
));
0
Alessandro Staffolani On

I solved in this way:

<script type="text/javascript">
$(function(){
    $('form[name=pneumatico]').find('input[name=prezzo]').keyup(function() {
        $(this).val($(this).val().replace('.', ','));
    });
    $('form[name=pneumatico]').find('input[name=sconto]').keyup(function() {
        $(this).val($(this).val().replace('.', ','));
    });
}); </script>

Thanks