Answer the question
In order to leave comments, you need to log in
Yii2. How to validate the properties of different models depending on each other?
How to validate the properties of different models depending on each other?
For example, the end date of model 2 must be greater than the start date of model 1.
Answer the question
In order to leave comments, you need to log in
Write your own validation rule. and then use in similar code.
if ($model1->validate()) {
$model2->temp_end = $model1->end_date;
if ($model2->validate()) {
// your code
}
}
write your own validator. in rules for the date of the second model
public function rules()
{
return [
....
[['dateEnd'], 'dateValidate'],
];
}
public function dateValidate($attribute, $params)
{
$model=Model_1::findOne(id);
if($this->dateEnd<=$model->dateStart){
$this->addError('dateEnd', 'Ошибка');
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question