Check box collecting of the data

I have very weird behaviour of the checkbox. I am doing a page in YII2 where I have some checkbox with different options, I need to collect all of them and add to Data Base. My problem is that I can’t collect any data. the view have this form.





      <?php $form1 = ActiveForm::begin(); ?>

        <?= 

             DetailView::widget([

            'model' => $model,

            'condensed' => false,

            'enableEditMode' => false,

            'hover' => true,

            'labelColOptions' => ['style' => 'width:20%; text-align:left !important; '],

            'mode' => DetailView::MODE_VIEW,

            'vAlign' => DetailView::ALIGN_MIDDLE,

            'valueColOptions' => ['style' => 'vertical-align: middle; '],

            'panel' => [

                'heading' => Yii::t('api', 'Description_a'),

                'type' => 'company',

            ],

          

            'attributes' => [

                [

                    'group' => true,

                    'label' => Yii::t('api', 'actions_grup1'),

                    'rowOptions' => ['class' => 'info']

                ],

         ['label' => Html::checkbox('101', false, ['label' => Yii::t('api', 'Action1')]),

                    'value' =>Yii::t('api', 'Description_1'),

                ],

         ['label' => Html::checkbox('102', false, ['label' => Yii::t('api', 'Action2')]),

                    'value' =>Yii::t('api', 'Description_2'),

                ],

              ],

            ],

        ]);

               <?= Html::a(Yii::t('api','create_new'), ['controller/create'], ['class' => 'btn btn-success' ]) ?>		

          <?php ActiveForm::end(); ?> 

even that all check box on definition have false on inspect I can see the value="1" and also in the function create in controller I have just this for now




     $post=Yii::$app->request->post();

    var_dump($post);

    exit;

the $post is array(0) { } no meter if I click or not to some checkbox to be checked.

Can someone tell me what I am doing wrong? Or what I am missing?

This seems weird. Do you have any validation rules in the model for attribute 101 or 102?

[size=“2”]Another note…I don’t know the scope of your project but i’d also be careful translating the values that are saved into the database. I would translate only the labels and save all of the values in one language. But like i said i don’t really know what your site does so it might not be best for you. [/size]

No I don’t have any, the scope is that admin can click and add ore not some priority. All this value 101,102 must be sent to one column in DB and whenever user is sending request for that action if he don’t have code of his function he will receive error. So my scope is to collect all ID of the checkbox and to insert them like one string.

here is the code of the checkbox view-source from page


<tr><th style="width: 20%; text-align: right; vertical-align: middle;"><label><input type="checkbox" name="101" value="1"> Api Update</label></th>

<td style="vertical-align: middle; "><div class="kv-attribute">This function will give </div>

</td></tr>

<tr><th style="width: 20%; text-align: right; vertical-align: middle;"><label><input type="checkbox" name="102" value="1"> Delete API</label></th>

<td style="vertical-align: middle; "><div class="kv-attribute">This function will give </div>

</td></tr>

Did you add any rules to see if it sent the POST?

what kind of rules you mean? can you give some example?

in your model every attribute has to have at least one rule for Yii to assign them.




public function rules() {

 return [

  ['101', 'integer'],

  ['102', 'string'],

 ]

}

an easier way to see the post instead of the var_dump and exit is to look in yii’s debug toolbar…

Open debug toolbar -> lastest (near the top) -> request (left side). Look under the POST section to see if your info is there.

the problem was in next part, the declaration of the action do not have to be in the button it have to be like this


<?php $form = ActiveForm::begin( ['action' => BaseUrl::to(['controller/createnew ']),  'method' => 'post',] ); ?>

with this the all checked checkbox will pass the id with value one, and the button have to be different type, it have to be submit.


echo Html::submitButton(Yii::t('api','create_new'),  ['class' => 'btn btn-success' ]);

so now I can collect all the data in my controller just like this




$post=Yii::$app->request->post();

 $checkbox= array_keys (  $post );

 foreach ( $checkbox as $value  ){

    //do what you need 

 }

Hope this post will help someone.