Multiple checkboxList with one model

Dear all,

has just started out Yii web app and encountered this problem.

What i am trying to achieve:

-To display a form with different panels with tags that can be selected from each category, however the user only need to choose one tag from any category

-user can select some items from panel 1, some from panel 2, etc and then click submit button to process.

Problem:

The validation is performed only for the first group (image in attachment):

In the image attached, the error should not happen because at least one checkbox is checked. However if i choose one the checkboxes from the first panel, the validation works well, ignoring the checkboxes from the second panel.

Basically, i want to display at least two checkboxLists that represent data from the same group. I also have problems with data submission because only the the last checkboxList is considered:

Some code from my _form.php:




(...)

        foreach($arrayCategories as $key => $value){

      ?>

            <div class="panel panel-default">

                <div class="panel-heading" role="tab" id="heading<?= $key ?>">

                    <h4 class="panel-title">

                        <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse<?= $key ?>" aria-expanded="true" aria-controls="collapse<?= $key ?>">

                        <?= $value ?>

                        </a>

                    </h4>

                </div>

                 <div id="collapse<?= $key ?>" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading<?= $key ?>">

                    <div class="panel-body">


                     <?php

                        $arrayTags = Tags::find()->where(['id_category'=>$key])->all();

                        

                        $arrayOpt = [];

                        foreach($arrayTags as $valueTags){

                            $arrayOpt[$valueTags->id_tag] = $valueTags->getTagsHasLanguages($valueTags->id_tag)[0]->name;

                        }

                        echo $form->field($modelExperienceTags[0], "id_tag[]")->checkboxList($arrayOpt);


                     ?>

(...)



By some reason, yii2 cannot handle form elements with the same name. Can anyone give me a solution to this problem?

Thanks

In this case you should create your own custom validator that would be able to check for the proper conditions. They are easy enough to create. See: http://www.yiiframework.com/doc-2.0/guide-input-validation.html

Thanks for your reply. I think custom validation will not solve my problem, since both checkboxelists are not related by yii 2. If i apply a specific rule over the model, then the rule will be applied twice (one for each checkbox group). The rule should be applied to both checkboxes since they represent the same model.

Thanks