yii2: make checkbox to be checked

好久不见. 提交于 2019-12-22 04:36:19

问题


I am using Yii2 framework and I'd like to generate an html code like this

<input type="checkbox" id="queue-order" name="Queue[order]" value="1" checked>

in a view which uses ActiveForm.

I've tried

echo $form->field($model, 'order')
          ->checkBox(['label' => ..., 'uncheck' => null, 'checked' => true]); 

as well as

echo $form->field($model, 'order')
          ->checkBox(['label' => ..., 'uncheck' => null, 'checked' => 'checked']); 

but desired string "checked" does not appear in the generated html code.

Strangely enough, if I substitute "checked" with "selected"

echo $form->field($model, 'order')
          ->checkBox(['label' => ..., 'uncheck' => null, 'selected' => true]); 

then generated html code contains attribute "selected":

<input type="checkbox" id="queue-order" name="Queue[order]" value="1" selected>

So, how can I generate html code for a checkbox with attribute "checked"?


回答1:


I guess this checkbox will be checked only if $model->order property take true value and if it has false (0 or null or false etc) value - field will be unchecked.




回答2:


if your are setting an external value in checkbox.

<?php $model->order = "02256"; ?>
<?= $form->field($model, "order")->checkbox(['value' => "02256"]); ?>



回答3:


echo $form->field($model, 'Status')->checkbox(['uncheck' => 'Disabled', 'value' => 'Active']);


来源:https://stackoverflow.com/questions/23981627/yii2-make-checkbox-to-be-checked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!