I would like to compare one of a newly created model's attributes against a related model's attribute when validating. I'm searching since a while and do not really find any useful informations.
In one of my other models, when updating, this piece of code is working (in the model):
public function compareWithDescendantsKeszDb() {
$gyermek = $this->descendants(1)->find();
if ($gyermek <> null) {
if ($gyermek->keszDb < $this->keszDb) {
$this->addError('keszDb', Yii::t('validation', 'some error message'));
}
}
}
public function rules() {
return array(
array('keszDb', 'compareWithDescendantsKeszDb'),
);
}
but the same approach doesn't seem to be working now, maybe it's because it's a create function now. I've tried like so (also in model):
public function compareWithSzeriaGyartmanyDb() {
$szeriaGyartmany = SzeriaGyartmany::model()->findByPk($this->szeriaGyartmanyId);
if ($this->db > $szeriaGyartmany->db) {
$this->addError('db', Yii::t('validation', "Error: maximum you can save is $szeriaGyartmany->db"));
}
}
public function rules() {
return array(
array('db', 'compareWithSzeriaGyartmanyDb'),
);
}
I hope it's somewhat clear what I would like to achieve. Can somebody please point me to the right direction? Thanks a lot!
BR c
Try using the safe attribute to validation as shown
public function rules() {
return array(
array('db', 'compareWithSzeriaGyartmanyDb','safe'),
);
}