0

I'm trying to use custom template for yii2 checkboxlist I'm using the code:

<?=
 $form->field($model, 'fruit_ids')->checkboxList($fruits, [
        'item' => function($index, $label, $name, $checked, $value) {
            return "<label class='col-md-4'><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</label>";
        }
]);
?>

Which output:

<div id="testform-fruit_ids">
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="0">Apple</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="1">Banana</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="2">Orange</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="3">Pear</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="4">Pineapple</label>
</div>

But I want to add attribute class="btn-checkbox options" and data-toggle="button" to <div id="testform-fruit_ids"> wrapper div element, i.e. I want the output like:

<div id="testform-fruit_ids" class="btn-checkbox options" data-toggle="button">
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="0">Apple</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="1">Banana</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="2">Orange</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="3">Pear</label>
    <label class="col-md-4"><input type="checkbox" name="TestForm[fruit_ids][]" value="4">Pineapple</label>
</div>

Please tell me the correct way to do it.

3 Answers 3

1

This should work:

<?= $form->field($model, 'fruit_ids')->checkboxList($fruits, [
        'item' => function($index, $label, $name, $checked, $value) {
            return "<label class='col-md-4'><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</label>";
        },
        'class' => 'btn-checkbox options',
        'data-toggle' => 'button'
   ]); ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank. How many attribute I can add like this and how Yii determine item content should placed inside div and class should placed inside div start tag.
Only limit there is PHP and HTML limitation so it shouldn't be a problem with reasonable number of attributes. As for the Yii look at the code starting from checkboxList() method
1

update your code like below

<?=
 $form->field($model, 'fruit_ids')->checkboxList($fruits, ['class'=>"btn-checkbox options",'data-toggle'=>"button",
        'item' => function($index, $label, $name, $checked, $value) {
            return "<label class='col-md-4'><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</label>";
        }
]);
?>

add your class 'class'=>"btn-checkbox options" and 'data-toggle'=>"button" into the array.

Comments

-1

You can use the following solution to add attribute class="btn-checkbox options" and data-toggle="button" to the <div id="testform-fruit_ids"> wrapper div element:

$form->field($model, 'oFacilities')->checkboxList(Facility::forWidget(),[
    'item' => function($index, $label, $name, $checked, $value) {
        $checked = $checked ? 'checked' : '';
        return "<label class='checkbox col-md-4' style='font-weight: normal;'><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</label>";
    }
])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.