checkboxList option "checked"

I can’t get the “checked” option of checkboxList to work:




<?= $form->field($profile, 'nonprof_array')->checkboxList($nonprof_orgs, ['checked' => 1]) ?>



I’ve also tried:




checkboxList($nonprof_orgs, $options=['checked' => 1])

//and

checkboxList($nonprof_orgs, ['checked' => TRUE])



But the checkboxes remain unchecked. Ultimately I want to pass in an expression as explained in http://www.yiiframework.com/forum/index...148525

Sorry for the dumb question. I know missing something obvious.


$profile->nonprof_array

must already have values. For example, if


$nonprof_orgs = ['yes', 'no'];

and


$profile->nonprof_array = ['yes'];

then checkbox ‘yes’ will be checked.

I think I understand what you’re saying, but it doesn’t seem to work. I pull a value from




$nonprof_orgs 



and load it into




$profile->nonprof_array 



before rendering the view, but the checkbox still remains unchecked. The way I understand the post I link to above, is that the ‘checked’ property needs to be set for each checked checkbox in the list, and therefore the need for an expression which evaluates to true or false for the checked property.

just fill $profile->nonprof_array with needed "checked" values and then checkboxlist will be with "checked" inputs

That’s what I have done and it’s not working.

Controller:




return $this->render('form', ['profile' => $profile, 'nonprof_orgs' => $nonprof_orgs, 'nonprof_array' => ['Org1', 'Org2']]);

// $nonprof_orgs contains ['Org1', 'Org2', 'Org3', ...]



View:




<?= $form->field($profile, 'nonprof_array')->checkboxList($nonprof_orgs, ['checked' => 1]) ?>



Still, nothing is checked.




$profile->nonprof_array = [500];

echo $form->field($profile, 'nonprof_array')->checkboxList([100 => 'abl', 500 => 'bla']);



Here is example. Checked ‘bla’

You are right. I’m not sure where this is breaking down in my code. Many thanks for your help.

EDIT: It works now. It was a combination of a few things, mainly incorrectly passing a populated variable $nonprof_array instead $profile->nonprof_array. Thanks again.