M
M
Maxim2018-09-28 20:58:54
Yii
Maxim, 2018-09-28 20:58:54

Yii validation on dependent fields?

I can't figure out what I'm doing wrong. It seems to be doing everything right, but it gives a validation error.
It is necessary for me that when the fields of one of the dates are not empty, then the second field gives an error if it is not filled. And when the fields are empty - do not issue an error for mandatory filling

[['date_from', 'date_to'], 'required', 'when' => function() {
                return $this-> date_from !== null || $this-> date_to !== null ;
            }],

And it also does not work when another field is equal to a certain value. Either way it throws an error!
[['description'], 'required', 'when' => function() {
                return  (int) $this->status == self::STATUS_REFUSED;
            }],

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2018-09-29
@myks92

Goodnight.
You should try this design.
Should work.

['date_to', 'required', 'when' => function($model, $attribute){
                return $model->date_from == 1;
            }, 'whenClient' => "function (attribute, value) {
                   return $('#uploadsfiles-date_from').val() == 1;
            }"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question