Conditional Validation (Validations based on other fields)

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 :
Salary” required if the field is “Working”

For only server side validations (Code should be included in rules() function of the respective model)

['salary', 'required', 'when' => function($model) {
        return $model->status == 'Working';
}]

Server + Client side validations (Code should be included in rules() function of the respective model)

['salary', 'required', 'when' => function ($model) {
	return $model->status == 'Working';
}, 'whenClient' => "function (attribute, value) {
	return $('#status ').val() == 'Working';
}"]
Share

You may also like...