How to create checkbox group in yii2?
That's what we need
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="checkbox" autocomplete="off" checked> Checkbox 1 (pre-checked)
</label>
<label class="btn btn-primary">
<input type="checkbox" autocomplete="off"> Checkbox 2
</label>
<label class="btn btn-primary">
<input type="checkbox" autocomplete="off"> Checkbox 3
</label>
</div>
That's what I have
<?
$options = ['uncheck'=>0];
echo ButtonGroup::widget([
'options' => [
'data-toggle' => 'buttons'
],
'buttons' => [
$form->field($model, 'field1')->checkbox($options),
$form->field($model, 'field2')->checkbox($options),
$form->field($model, 'field3')->checkbox($options),
],
]);
?>
What I need to add in my code, to generate that markdown?
My variant. I used standard yii radiobox and customize template.
<?= $form->field($model, 'attribute')->radioList(
[
1 => 'Enabled',
2 => 'Disabled'
],
[
'item' => function ($index, $label, $name, $checked, $value) {
if ($value==1)
$class_btn = 'btn-success'; // Style for enable
else
$class_btn = 'btn-default'; // Style for disable
if ($checked)
$class_btn = $class_btn.' active'; // Style for checked button
return
'<label class="btn '. $class_btn.'">' . Html::radio($name, $checked, ['value' => $value]) . $label . '</label>';
},
'class' => 'btn-group', "data-toggle"=>"buttons", // Bootstrap class for Button Group
]
)->label('Some label');
?>
My result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With