activeCheckboxlist Usage

hey everyone,

I am a bit thrown off by activeCheckboxlist.

I have a sign up form with a table behind it (let’s say) with two text fields:

  • Username

  • Password

And about 7 fields prefixed with "interest_". These prefixed fields represent the interests of the user signing up.

Maybe that’s not the best idea but it would also apply if say the interests was another table connected to the user and I wanted to display that as a checkboxlist. So I am trying to just transpose a list of values onto a models attributes really.

How can I turn those prefixed fields into a checkboxlist for saving in the model and handle them in the model (it is handling them in the model that is confusing me)?

Or is it that I am thinking totally wrong of checkboxlist and they are designed for the something very specific that I have just over looked?

Thanks in advance,

I think you can use a list box instead of checkbox list…


<?php echo $form->ListBox($model,'YOur field name',CHtml::listData(Your model name::model()->findAll('ID', 'value'),array('multiple' => 'multiple')); ?>

i think this will help you…

Thanks for the reply but evne that dont use a part of the parent model, it works disjointed from the parent model.

Imagine I have one form with one model, how can I make a part of the model a checkboxlist and handle it in the model or do checkbox lists different usage?

But it also applies to list fields of a sub part of the model, so say I wanted to display a multi select part of my main model for the form of the fields prefixed with "interests_". I am just a bit turned around by it cos I can provide a list of values but I am not sure if I can provide a list of attributes and have the model translate them right.

Correct, activeCheckBoxList has a different usage. It represents a single attribute in your model (in other words, a single field in your table). It allows multiple selections so one use would be to serialize the values selected into your field.

I think that what you need to do is generate a single activeCheckBox for each of your 7 fields.

http://www.yiiframework.com/doc/api/1.1/CHtml#activeCheckBox-detail