I have in database (Mysql) few decimal type fields and I want to input numbers with dot (##.##) or comma (##,##) but store only with dot. I used decimali18nbehavior but this convert numbers in beforeValidate and this is for me too late. I can overwrite CActiveRecord::setAttribute method but this is not elegant and I want reuseable solution.
My approach:
Extends CActiveRecord class and create onAfterSet event
class ActiveRecord extends CActiveRecord
{
public function setAttribute($name, $value)
{
if(parent::setAttribute($name, $value))
{
$this->afterSet($name);
return true;
}
return false;
}
protected function afterSet($name)
{
if($this->hasEventHandler('onAfterSet'))
{
$event=new CModelEvent($this, $name);
$this->onAfterSet($event);
return $event->isValid;
} else
return true;
}
public function onAfterSet($event)
{
$this->raiseEvent('onAfterSet',$event);
}
}
And create Behavior to handling this event (and show field)
class DecimalBehavior extends CActiveRecordBehavior
{
public function events()
{
return array_merge(parent::events(), array('onAfterSet'=>'afterSet'));
}
public function afterSet($event)
{
// Check if field is decimal and change comma to dot, eventualy add validators
}
I wonder if this is correct solution or maybe it’s is more finess solution.