Activeform Checkboxlist

Hi,

I have comma separated values in a $model and have been trying to place them into a checkboxlist independantly.

I placed the value into a variable as below


 $name = $model->name;

Next I wanted to place them into a checkboxList and put them as such


echo $form->checkBoxList($model, 'name', array($name=>'name'));

What I got as the output was a checkbox labelled ‘name’ only.

How do I get a list of all the values in the $name array as checkboxlist instead?

Consider this :




<?php

$model = new Person();

$form = new CActiveForm();

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

echo $form->checkBoxList($model, 'name', array('name A'=>'name A','name B'=>'name B','name C'=>'name 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

[color="#FF0000"]+1 if you are happy with my answer[/color]

Hi UwaisFariz,

Thanks for your reply. The solution posted seems to have a static value for ‘name’. I have a dynamic value for ‘name’ according to user inputs hence the output might contain 1 or more values depending on the user input prior to registration in earlier modules.

I’m pretty sure the solution is a simple hack into the plugin values as everything is already there. I might not be using the syntax correctly perhaps?

If I were to echo $name, directly, I would get these values


echo $name;

Doberman,russel,terrier,chihuahua

All strings separated by commas.

Ok I’ve managed to solve the problem.

Here’s the solution.

Basically since I originally had an array of strings, I discovered that the array was identified not by the delimiter but by character hence assuming the array has the following


$name = $model->name

echo $name;

This would result in Doberman,russel,terrier,chihuahua. But when I rechecked the values


echo $name[0];

This resulted in just D as the string.

Hence I rephrased the array with this


$name = explode(","$model->name);

echo $form->checkBoxList($model,'name',$name);

Now there is another issue.

The checkbox generated is all well and good but now when checked it is not giving its value and generating and error instead.

Following is the code


<td style="vertical-align:text-top;"><label class="error" for="name">Name</label></td>

			<td>

				<input id="name" type="hidden" value="" name="[name]" /><span id="bpname"><input id="name_0" class="error" value="0" type="checkbox" name="Annletter[bpname][]" /> <label style="display: inline;" for="name_0">CSS</label><br/>

<input id="name_1" class="error" value="1" type="checkbox" name="[name][]" /> <label style="display: inline;" for="name_1">PERL</label><br/>

<input id="name_2" class="error" value="2" type="checkbox" name="[name][]" /> <label style="display: inline;" for="name_2">PHP</label><br/>

<input id="name_3" class="error" value="3" type="checkbox" name="[name][]" /> <label style="display: inline;" for="name_3">HTML</label></span> 

				

				<div class="errorMessage">Name is invalid.</div>			</td>

		</tr>

Whats wrong here?