How do I fix user input before validating / saving?
1 answer
It would probably be easier if you used CFilterValidator
to automatically convert the input from a string to a float when validation occurs; the rest must take care of themselves.
So your model would have
public function rules() {
return array(
array('price', 'filter', 'filter' => 'convertToFloat'),
);
}
and the filter function will look like
function convertToFloat($value) {
return floatval(trim(str_replace(',', '.', $value)));
}
You can also specify the filtering function as any valid callable , for example make it a model method and specify it as
'filter' => array($this, 'convertToFloat')
+8
source to share