E
E
EVOSandru62015-08-26 07:15:06
Yii
EVOSandru6, 2015-08-26 07:15:06

Is it possible to put a validator in rules in yii according to a certain condition?

Good afternoon,
There is a rule:

,

But I want it to work only if I have a specific role_id in my get parameters ?
website/registration?role_id=2
Is this possible?
Of course, you can break it into 2 scenarios and transfer part of the obligations to it, but I wonder if it is possible to do such things within the framework of one ?!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Klyuev, 2015-08-26
@EVOSandru6

1. You can write your own validator and check whatever your heart desires in it.
2. Before validation, you can initialize the model with the passed role_id and use additional conditions in the validator, like:

[
    ['state', 'required', 'when' => function($model) {
        return $model->role_id == '2';
    }],
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question