Conditional Validation Question

My form fails to submit if I have a value other than RE in my account field. The account field is a radio button value.




    public function rules()

    {

        return [

            ['username', 'filter', 'filter' => 'trim'],

            ['username', 'required'],

            ['username', 'email'],

            ['username', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This email address has already been taken.'],

            

            ['name', 'filter', 'filter' => 'trim'],

            ['name', 'required'],

            ['name', 'string', 'min' => 2, 'max' => 255],


            ['password', 'required'],

            ['password', 'string', 'min' => 6],

            

            ['account', 'required'],

            ['account', 'string','min' => 1, 'max' => 2],

            

            ['license', 'required', 'when' => function($model) {

		return $model->account == 'RE';

            }],

            ['license', 'string','min' => 1, 'max' => 255],

        ];

    }



I am missing something with conditional validation?

It’s because it’s a radio button (checkbox don’t work with cond. validators either) switch it to a dropdown and it will work. I have submitted an issue on github awhile back and i think Yii2 2.4 never mind 2.5 is going to fix it.

also you can use the following for trim too




 ['username', 'trim'],



Thanks.

I also had to use whenClient to make it work.




['license', 'required', 'when' => function($model) {

	return $model->account == 'RE';

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

	return $('#signupform-account').val() == 'RE';

}"],