The above answers are close here is a working and tested version.
<?=
$form->field($model, 'category_id')->checkboxList(\yii\helpers\ArrayHelper::map(common\models\BusinessCategory::find()->all(), "id", "name"), [
'onclick' => "$(this).val( $('input:checkbox:checked').val()); ",//if you use required as a validation rule you will need this for the time being until a fix is in place by yii2
'item' => function($index, $label, $name, $checked, $value) {
return "<label class='ckbox ckbox-primary col-md-4'><input type='checkbox' {$checked} name='{$name}' value='{$value}' tabindex='3'>{$label}</label>";
}
])
?>
The above are missing checked and the label. Without the label wrapping it and not setting any attributes it will only allow for the checkbox to be checked by clicking the box. This way will still result in default behavior of clicking the box or the label to check it. Without the checked var being used you will not be able to set a checked value in the controller if need be as well.
The above will work for radio list as well. You will have to change change type=checkbox to type=radio and make it use yii2’s radio list instead of checkbox list.
That’s perfect. Thank you so much @Grischer and @skworden for your valuable reply. I got it working perfectly.
I want to add another feature in checkboxlist. I have categories table and category may have child categories. So How can I create checkboxlist like tree structure for this categories.