Yii2: check the box

I use the Yii2 structure and I would like to generate html code like this

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

in a view that uses ActiveForm.

I tried

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

and

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

but the desired "checked" line is not displayed in the generated HTML code.

Oddly enough, if you replace "checked" with "selected"

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

then the generated html code contains the attribute "selected":

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

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

+8
source share
3 answers

I assume that this flag will only be checked if the $model->order property is true , and if it is false ( 0 or null or false , etc.), then the value will be unchecked.

+5
source

if you set the external value in the checkbox.

 <?php $model->order = "02256"; ?> <?= $form->field($model, "order")->checkbox(['value' => "02256"]); ?> 
+2
source
 echo $form->field($model, 'Status')->checkbox(['uncheck' => 'Disabled', 'value' => 'Active']); 
0
source

Source: https://habr.com/ru/post/970149/


All Articles