Model validate field when other field is filled?

Hi guys,

Have an issue here.

I have two field in a model that are more or less bind together. I want to validate an attribute in my model when the other attribute is filled and the other way around.

For example:

field x is filled -> field y can not be empty. When field y is filled, field x can not be empty.

I know I can make a scenario, but how can I make this work with an active form? Is this actually possible or do I have to write my own javascript/jquery code in order to do this?


"Conditional Validation

To validate attributes only when certain conditions apply, e.g. the validation of one attribute depends on the value of another attribute you can use the when property to define such conditions. For example,"

[[‘first_name’,‘last_name’, ‘gender’, ‘date_of_birth’, ‘mobile_no’,‘address’], ‘required’, ‘when’ => function ($model) {

        return $model->type == Person::STUDENT;

        }, 'whenClient' => "function (attribute, value) {

        return $('#type').val() == '2';  // return $('#type').val() == 'india'


write this in your rule,it is conditional validation

#type is html tag id

Thank you guys. I was looking for this, but I guess I did not look good enough. :slight_smile: