Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "." for decimals in zend validator float

I have a form with a element called "price". I validate this element with the "float" validator. The thing is when I insert, for example:

12,50 => it is valid but when I try to save it on the DB (mysql) it is saved as "12.00"

So I wanna to change the decimal character from "," to ".". Does anybody knows how??

Note. If I put:

$price->addValidator('Float', 'de')

or

$validator = new Zend_Validate_Float(array('locale' => 'de'));
$price->addValidator($validator)

It does not work.

like image 763
José Carlos Avatar asked May 18 '26 04:05

José Carlos


1 Answers

You can use a filter Zend_Filter LocalizedToNormalized to it will normalized you localized price according to the user's locale.

A typical price element would be like this one:

$price = new Zend_Form_Element_Text('price');
$price->setLabel('Price:')
      ->setRequired(true)
      ->setAttribs(array('required name' => 'price', 'maxlength' => '12'))
      ->addFilter('StripTags')
      ->addFilter('StringTrim')
      ->addFilter('pregReplace', array('match' => '/\s+/', 'replace' => ''))
      ->addFilter('LocalizedToNormalized')
      ->addValidator('stringLength', true, array(1, 12))
      ->addValidator('float', true, array('locale' => 'en_US'))
      ->addValidator('greaterThan', true, array('min' => 0));
$this->addElement($price);

Of course, you can improve it and add the validators/filters you need.

like image 149
Liyali Avatar answered May 20 '26 01:05

Liyali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!