Using “.” for decimals in zend validator float

微笑、不失礼 提交于 2019-12-07 17:47:44

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/9299012/using-for-decimals-in-zend-validator-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!