Yii2: ActiveField::checkbox() - There's no way to force check/uncheck.

Created on 14 Jul 2016  路  6Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

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,
]);?>

What is the expected result?

I expect to see the checkbox checked.

What do you get instead?

The checked option is ignored.

Additional info

| Q | A |
| --- | --- |
| Yii version | 2.0.9 |
| PHP version | 7.0.8 |
| Operating system | CentOS 7 |

hacktoberfest ready for adoption bug

Most helpful comment

This method will generate the "checked" tag attribute according to the model attribute value.

Source

This means the following:

$model->myAttribute = true; // or 1, or '1'

echo $form->field($model, 'myAttribute')->checkbox(); // checked ckeckbox

All 6 comments

This method will generate the "checked" tag attribute according to the model attribute value.

Source

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MUTOgen picture MUTOgen  路  3Comments

skcn022 picture skcn022  路  3Comments

nokimaro picture nokimaro  路  3Comments

Locustv2 picture Locustv2  路  3Comments

AstRonin picture AstRonin  路  3Comments