3

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.

José Carlos
  • 1,005
  • 16
  • 29
  • 1
    I suspect that your database thinks it's in a different country. What database are you using? You can add a tag for it. – DOK Feb 15 '12 at 18:26
  • Do you mean you want to change the decimal character from "." to "," when you save it into your database? Having you data formated in the english way in your database is more appropriate than having them localized in the user's locale. – Liyali Feb 15 '12 at 19:07

1 Answers1

11

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.

Liyali
  • 5,643
  • 2
  • 26
  • 40
  • In fact, I do not wanna localize it. I just wanna use float as validator for numbers like 123.4, I do not wanna use commas. – José Carlos Feb 15 '12 at 20:39
  • 1
    Then you should use a locale that doesn't accept commas as a valid float separator, such as en_US. Here is the line you're looking for: `->addValidator('float', true, array('locale' => 'en_US'))` – Liyali Feb 16 '12 at 02:34