[[+content_image]]
M
M
Mikha Pankratov2015-12-22 01:52:05
Yii
Mikha Pankratov, 2015-12-22 01:52:05

Why is validate(compare 2 fields) not working?

Good night, I
will immediately describe the problem: I am
doing the validation of 2 fields or 1 or 2 must be filled!

[['number', 'email'], 'required', 'when' => function($model) {
          if($model->email == '' && $model->number ==''){
            return false;
          } else {
            return true;
          }
        }, 'enableClientValidation' => false],

Validation takes place with 2 empty fields) as if I didn’t write the function at all. Maybe someone met with this?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
S
Sergey Lysogor, 2015-12-22
@serhioli

That's right - when will only apply the require validator if it returns true. You have both fields empty (it's "iron" true) and you return false - the validator is not applied.
Try like this:

[['number', 'email'], 'required', 'when' => function($model) {
          return ($model->email == '' && $model->number =='');
        }, 'enableClientValidation' => false],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question