AleCat83 AleCat83 - 1 year ago 140
PHP Question

Conditional validation, if a checkbox is checked other fields are required

I need to validate a form in the following way:

I have a checkbox and 3 text fields, if the former is checked the latter are required.

Is it possible to achieve this "out of the box"?

Answer Source

Yes this is possible. I recommend reading the Yii Docs about input validation and especially the part about conditional validation:

I guess you'll end up using something like (untested):

    ['my_attribute', 'required', 'when' => function($model) {
        return $model->my_checkbox === true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download