Set Checkbox As Checked In Activeform

Dear friends,

I feel myself a little crazy, but I can not manage to make a checkbox to be checked. 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]); 



and


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 I can generate html code for a checkbox with attribute "checked"?

Refer the API docs. The active field checkbox will generate the "checked" tag attribute according to the model attribute value. So you simply set the default value of the model attribute, to true before you render your view.




// for example in controller action

$model->order = true;

return $this->render('_form', ['model'=>$model]);



Thanks! Now I see that I mistakenly used ActiveForm instead of Html helper to generate checkbox. For the sake of simplicity, I omitted some details in my question. One of them was that the checkbox was related not to a property of the model but to a property of an associated model. For this reason it is better to use Html helper than ActiveForm.