V
V
Vladimir2017-09-04 10:44:17
Yii
Vladimir, 2017-09-04 10:44:17

How to make one of the checkboxes mandatory Yii2?

Good day! I have 5 fields with checkbox type. How can I do it in Yii2 so that if at least one of them was filled, then the validation is successful, otherwise, say that select at least one checkbox. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2017-09-04
@MasterGerold

www.yiiframework.com/doc-2.0/yii-validators-valid...

[['check1','check2','check3'],'required','when'=>function ($model) {
    return ($model->check1 == null and $model->check2 == null and $model->check3 == null);
}, 'message'=>'чудак, чекни хоть что-нибудь']

O
oh, 2017-09-04
well @AnneSmith


write a validation javascript: check the values ​​of all checkboxes and if everything is not checkd, then show an error message

M
melnikov_m, 2017-09-04
@melnikov_m

Change checkbox to radio and make it required

$form->field($model, 'radioAttribute')
    ->radioList([
        '1' => 'Первый',
        '2' => 'Второй',
        '3' => 'Третий'
    ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question