Using Form Builder to view & populate a cheboxlist

Hi, I have ModelA and ModelB with relation MANY_MANY.

I’m using form builder to create form for ModelA, and I need to view a list of checkboxes (checked and/or not checked) with id/values taken from ModelB.

I’ve enstablished relation using relations() function, but I still don’t understand how to view relational checkboxes.

I’ve tried with this function in ModelA:

 public function getItems(){

                $model = ModelB::model()->findAll();

                foreach($model as $k=>$v):

                $arr[$v->id] = $v->name; 


                 return $arr;


and in form:





It works in the sense that it displays all items from ModelB but -of course- it doesn’t work as I want because it doesn’t display checked boxes, and it doesn’t update table that joins ModelA with ModelB. Maybe I have to write code to do that? I mean to know if a check is checked in getItems function and an INSERT statement in a afterSave() function to update join table?



It seems to me that it’s not possible to check/uncheck checkboxes generated by ‘checkboxlist’.

I’ve tried with something simple like this:





It display all 4 checkboxes but I’ve tried for a few hours to make checkboxes 1 & 3 checked and it seems not possible. I hope I’m doing something wrong because i cannot believe that checkboxlist doesn’t implement a way to render checkboxes checked/unchecked. Either with ‘attributes’ key (that is passed as htmlOptions) doesn’t work because it assigns key/value couple to all checkboxes… Just to be clear:






it assigns selected=“selected” to all checboxes. I don’t know why it doesn’t work with ‘checked’ value, that it’s the correct one…

Anyway, is it possible that this problem is only mine? No one needs to check/uncheck checkboxes in a checkbox list?

I’ve opened a ticket…

I’m looking to do the same thing and have not found good documentation on how to perform this. Zitter, did you ever find a solution?

already a solution to default check the categories my post belongs to:

<?php echo $form->checkboxList($model, 'categories', CHtml::listData(Category::model()->findAll(), 'id_category', 'category'), array('template' => '<li>{input} {label}</li>', 'separator' => '', 'checkAll' => 'Alle rubrieken')); ?>