O
O
oleg_462015-12-26 01:47:50
Yii
oleg_46, 2015-12-26 01:47:50

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

2 answer(s)
D
Dmitry Bay, 2015-12-26
@kawabanga

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
}
}

I
Ilya, 2015-12-26
@Raz-Mik

write your own validator. in rules for the date of the second model

public function rules()
    {
        return [
            ....
            [['dateEnd'], 'dateValidate'],
        ];
    }

then the validator itself
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 question

Ask a Question

731 491 924 answers to any question