Model Conditional Validation


I’m trying to validate conditionally two fields, in view I have:

<?= $form -> field ($model,‘employee_nino’) ?>

<?= $form->field($model, ‘employee_nonino’)->checkbox() ?>

and in Model rules() I have:

[‘employee_nino’, ‘required’, ‘when’ => function($model) {return $model->employee_nonino == 1;},‘message’ => ‘Shit.’],

For some reason it ALWAYS requires employee_nino, even when employee_nonino is set to 1

Any hints on this?

You may need to add

['employee_nino', 'required', 'when' => function($model) {return $model->employee_nonino == 1;},'skipOnEmpty' => true,

				'skipOnError' => true,'message' => 'Shit.'],



Thanks for the info but it still doesn’t want to disable field validation on ‘when’ condition, so it always requires that field.

Any other hints?

If you replace return $model->employee_nonino == 1; with return false; do you get the same behaviour?


Yes, I tried everything, it always REQUIRES the field to be completed, no matter what condition is there and if it returns true or false.

Should be

['employee_nino', 'required', 'when' => function() {return $this->employee_nonino == 1;},'skipOnEmpty' => true,

                                'skipOnError' => true,'message' => 'Shit.'],

Strike my last reply, I’m not sure that works, sorry, gonna go check some of my own rules now!

Strange but it doesn’t work for me :(

So disappointed about that. maybe there’s a bug?



If you still need help, please post your entire ruleset here

public function rules()


    return [

		[['employee_nino'], 'required', 'when' =&gt; function(&#036;model) {return &#036;model-&gt;employee_nonino == 1;},'skipOnEmpty' =&gt; true, 'skipOnError' =&gt; true,'message' =&gt; 'Shit.','on' =&gt; ['personalupdate']],

		[['employee_nationality','employee_marital_status','employee_workpermit','employee_title','employee_firstname','employee_lastname', 'employee_email', 'employee_phone', 'employee_mobile', 'employee_address', 'employee_town', 'employee_postcode','employee_country'], 'required', 'on' =&gt; ['personalupdate']],

		[['employee_absence_days','employee_absence_periods','employee_medication','employee_treatment','employee_injury','employee_disability', 'employee_p46'], 'required', 'on' =&gt; ['medicalupdate']],

		[['employee_worktime','employee_details_complete','employee_details_held'], 'compare', 'compareValue' =&gt; true,'message' =&gt; 'This is required'],


		[['employee_kin_firstname','employee_kin_lastname','employee_kin_contactno','employee_kin_relationship','employee_kin_address','employee_kin_postcode'], 'required', 'on' =&gt; ['nextkinupdate']],


		[['employee_bank_holder_name','employee_bank_name','employee_bank_sortcode','employee_bank_account_number','employee_nobank'], 'required', 'on' =&gt; ['bankaccountupdate']],



		[['employee_user_id'], 'required'],

        [['employee_user_id', 'employee_title', 'employee_country', 'employee_nationality', 'employee_nino', 'employee_marital_status', 'employee_p46'], 'integer'],

        [['employee_workpermit', 'employee_convict', 'employee_med_examination', 'employee_declaration', 'employee_nonino', 'employee_nobank', 'employee_details_complete', 'employee_details_held', 'employee_medication', 'employee_treatment', 'employee_injury', 'employee_disability', 'employee_worktime'], 'boolean'],

        [['employee_dob'], 'safe'],

        [['employee_firstname', 'employee_lastname', 'employee_email', 'employee_address', 'employee_bank_holder_name', 'employee_bank_name', 'employee_bank_sortcode', 'employee_bank_account_number', 'employee_kin_firstname', 'employee_kin_lastname', 'employee_kin_contactno', 'employee_kin_relationship', 'employee_kin_address', 'employee_kin_postcode', 'employee_absence_days', 'employee_absence_periods'], 'string', 'max' =&gt; 255],

        [['employee_phone', 'employee_mobile', 'employee_postcode'], 'string', 'max' =&gt; 10],

        [['employee_town'], 'string', 'max' =&gt; 32],

        [['employee_convict_description'], 'string', 'max' =&gt; 1024],

        [['employee_user_id'], 'unique']



The rule which doesn’t work is conditional one:

[[‘employee_nino’], ‘required’, ‘when’ => function($model) {return $model->employee_nonino == 1;},‘skipOnEmpty’ => true, ‘skipOnError’ => true,‘message’ => ‘Shit.’,‘on’ => [‘personalupdate’]],

employee_nino - is always required no matter what value has $model->employee_nonino, I have even tried to set it manually to make sure it gets value 1


Have somebody tried it? Still cannot figure out.

The issue is client side validation is not disabled. I had to figure this out today, so might be useful to someone! Or alternatively you can dynamically enable/disable client side validation I suppose.

            ['avalue', 'required', 'when' => function ($model) {
                    return ($model->anull == 'N');
                }, 'enableClientValidation' => false