问题
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