how to render CActiveForm checkBoxList ?

Hi Folks,

I am trying to render a checkBoxList and I’m stuck. I have a Person tbl whose type field is a varchar.

There are a fix number of type namely Type A, Type B and Type C.

Type field in the database is saved as comma separate such as Type A, Type B (i.e. this person is type A and type B )

so far, this is what I got in the view:




<?php echo $form->checkBoxList($model, 'type', array('type A'=>'Type A','type B'=>'Type B')); ?>



From reading the doc, I can not see how multiple checkboxes are checked when a person type field value is ‘type A, type B’

Please help.

Thank you.

Consider this:




<?php

$model = new Person();

$form = new CActiveForm();

$model->type= array('type A','type B');

echo $form->checkBoxList($model, 'type', array('type A'=>'Type A','type B'=>'Type B','type C'=>'Type C')); 

?>



You should set your model type attribute (on after find method ) to an array holding the person types

Then in the third parameter of checkboxlist you pass all the types that should appear in the list (checked or not)

Then the checkbox list is working as expected :D

thanks. it works.

hi ,

i asking if i using checkboxlist for marital status.

<?php echo $form->radioButtonList($model, ‘status’, array(‘married’ => 'Married ', ‘single’ => ‘Single’));?>

how i render the value for the status to show in view panel because the result always show the word Array if i if i choose married as my status…

i try type print_r ($model->status) and the value show is Array ( [0] => Married ) . How i can get the value for only Married.

Please help.

Thx

Just a guess… but does the data have to be formatted to use with RadioButtonList?

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

hi dniznick. thx for reply.

i still having same problem value is Array even change to checkBoxList.

<?php echo $form->checkBoxList($model, ‘status’, array(‘married’ => 'Married ', ‘single’ => ‘Single’));?>

what condition need to write or check at controllers side. Can gv me more further detail. =)

any1 can give me suggestion.

thx.

Since checkboxlist allows multiple selections it should return an array with the user selections.

So you have to iterate the array and pick the selected values.

If you want to allow one selection use radioButtonList

hi Spyros.

i still new in yii. can gv me some more example on write the array to return the user selection for this checkboxlist condition. i don’t know how to start the condition for user selection.

<?php echo $form->checkBoxList($model, ‘status’, array(‘married’ => 'Married ', ‘single’ => ‘Single’));?>

thx for reply. XD

In your case there should not be allowed multiple answers (Single or married not both)

So you should use




<?php 

echo $form->radioButtonList($model, 'status', array('married' => 'Married ', 'single' => 'Single'));

?>



and retrieve the status by $_POST["Model"]["status"]

if you want to use checkboxlist you retrive the checked options with




<?php 

foreach( $_POST["Model"]["status"] as $value){

	....

}

?>



As Spyros suggest, in your case, it is best to use ‘radio button’.

Anyway, your checkbox list


<?php echo $form->checkBoxList($model, 'status', array('married' => 'Married ', 'single' => 'Single'));?>

make sure the $model->status = array(‘married’).

Your post told me that your array is array(‘Married’) which does not match the value of your checkbox.

Hi,

how to make user only select 1 choice for this case. I not sure how to do it because the user now can select all the selection using checkBoxList. Is there any suggestion for it.

<?php echo $form->checkBoxList($model, ‘status’, array(‘married’ => 'Married ', ‘single’ => ‘Single’));?>

thx for reply. XD

See my post above for radiobuttonlist