nguyendh
(Duynguyen0511)
July 11, 2011, 9:35pm
1
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.
Spyros
(Spyros)
July 12, 2011, 4:14am
2
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
jowen
(Ljowen985)
July 14, 2011, 2:16am
4
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
dniznick
(Dniznick)
July 14, 2011, 3:28am
5
Just a guess… but does the data have to be formatted to use with RadioButtonList?
Generates the data suitable for list-based HTML elements. The generated data can be used in dropDownList, listBox, checkBoxList, radioButtonList, and their active-versions (such as activeDropDownList).
http://www.yiiframework.com/doc/api/1.1/CHtml#listData-detail
jowen
(Ljowen985)
July 14, 2011, 4:27am
6
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.
Spyros
(Spyros)
July 14, 2011, 6:48am
7
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
jowen
(Ljowen985)
July 14, 2011, 8:03am
8
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
Spyros
(Spyros)
July 14, 2011, 9:14am
9
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){
....
}
?>
nguyendh
(Duynguyen0511)
July 21, 2011, 3:20pm
10
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.
jowen
(Ljowen985)
July 22, 2011, 2:28am
11
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
Spyros
(Spyros)
July 22, 2011, 8:05am
12
See my post above for radiobuttonlist