O Connor O Connor -4 years ago 152
PHP Question

How to remove class from the DIV parents from checkboxList() in Yii framework 2

Below is my code to generate a checkbox list in Yii framework 2.

<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($model, 'my_property')->checkboxList(['A', 'B', 'C']) ?>
<?php ActiveForm::end(); ?>

This generates the follow HTML checkbox list.

<div class="form-group">
<label class="control-label" for="my_property">My Property</label>
<input type="hidden" name="Model[my_property]" value="">
<div id="model-my_property">
<div class="checkbox">
<input type="checkbox" name="Model[my_property][]" value="0"> A
<div class="checkbox">
<input type="checkbox" name="Model[my_property][]" value="1"> B
<div class="checkbox">
<input type="checkbox" name="Model[my_property][]" value="2"> C
<p class="help-block help-block-error"></p>

How can I remove the
from the
which directly is surrounding the
tag? I have tried
, with these I can only touch the outer

Answer Source

I guess you are using yii\bootstrap\ActiveForm and not default yii\widgets\ActiveForm because there is this element styled.

Easiest solution - use the second ActiveForm class so it will be unstyled.

And if you don't want to change ActiveForm class:

->field($model, 'my_property')
    ['A', 'B', 'C'],
    ['item' => function ($index, $label, $name, $checked, $value) {
        return '<div>'
                . '<label>'
                . '<input type="checkbox" name="' . $name . '" value="' . $value . '"> '
                . $label 
                . '</label>'
                . '</div>';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download