Having below code:
<?= $form->field($model, 'categories[]', ['options' => ['class' => 'xform-group']])->checkbox([
'uncheck' => null,
'class' => 'chk',
'id' => 'model-category_id_' . $category->id,
'label' => $category->name,
'labelOptions' => ['class' => 'chk full-width'],
'template' => '{input}{beginLabel}{labelTitle}{endLabel}{error}',
'value' => $category->id,
'checked' => true,
]);?>
I expect to see the checkbox checked.
The checked option is ignored.
| Q | A |
| --- | --- |
| Yii version | 2.0.9 |
| PHP version | 7.0.8 |
| Operating system | CentOS 7 |
This method will generate the "checked" tag attribute according to the model attribute value.
This means the following:
$model->myAttribute = true; // or 1, or '1'
echo $form->field($model, 'myAttribute')->checkbox(); // checked ckeckbox
@w3lifer - That still does not fix the issue i am referencing. It works fine for attribute directly, but if your attribute is an array then it does not work anymore. In 1.1.x versions you could set the checked attribute manually, this should be the case here too, so that my above use case to work properly.
how should a checkbox work for an array valued attribute?
maybe what you want is activeCheckboxList
?
but to fix this issue we could set checked
option only if it does not exist already here: https://github.com/yiisoft/yii2/blob/0ac161b69d77db3179cf17388aedd8df35246079/framework/helpers/BaseHtml.php#L723
still not working
<?php echo $form->field($model, "is_listable[$contentModel->id]")->checkbox(['label'=>'','class'=>'content_is_listable_class','checked'=>true]); ?>
this must be checked by default render but not working.
Most helpful comment
Source
This means the following: