Conditional validation - when not working

This is one of my validation rules for a model:
[‘my_field’, ‘required’, ‘when’ => function($model) {
return $model->status != 0;
}],
But the field is always required.
What am i doing wrong?
I tested with xdebug and the function does work, $model->status is 0.
I even changed the function to be a simple return TRUE; , but nothing

Can you show us all the rules of the model?
What comes to my mind is that you might have another rule that requires my_field.

I removed all other rules. Same result.

Well, that’s strange.
Are you sure that you are editing the rules of the model that is used in the form?

Yes, if i remove that rule everything works.

If you don’t want the field to be required you should try with false. Just guessing, is the value 0 or ‘0’?

Yes sorry, i meant return false

Client validation?
https://www.yiiframework.com/doc/api/2.0/yii-validators-validator#$whenClient-detail

1 Like

when does not afffect client validation, so you either have to specify whenClient or disable client validation for that field.

2 Likes

That was it. Thanks