CHtml::checkBoxList

Is there a way to disable a single checkbox in the checkBoxList?

Seems that a htmloptions parameter is applied to all checkboxes, but in a case of dropDownList I can set htmloptions for each element separately.

I’m not sure if this will fit your particular scenario but in your _form.php view file you can do this:


<div class="row">

	<?php echo CHtml::activeLabelEx($model,'enabled'); ?>

	<?php echo CHtml::activeCheckBox($model,'enabled', array('readonly'=>true)); ?>

	<?php echo CHtml::error($model,'enabled'); ?>

</div>

Note this bit: [, array(‘readonly’=>true)]

Hope this helps.

Thanks for your reply, outrage, but this works only for a CHtml::checkBox element.

I am talking about a CHtml::checkBoxList. This method looks like:


public static string checkBoxList(string $name, mixed $select, array $data, array $htmlOptions=array())

$htmlOptions paramter is applied to the all generated checkboxes. But in the CHtml::dropDownList there is an opportunity to set options for a particular element by passing $htmlOptions paramater like this:




$htmlOptions = array(

    'value1' => array('disabled'=>true, 'label'=>'value 1'),

    'value2' => array('label'=>'value 2'),

);



Of course I can generate checkboxes in a loop using CHtml::checkBox method. But I’m just curious why the checkBoxList doesn’t provide this feature like dropDownList or listData.

Bit late with this reply, but perhaps someone else will find it useful. I wanted to do the same thing and I couldn’t find a way to do it with the existing code. So I made the following change to the CHtml::checkBoxList function so that it works like the ‘options’ property in dropDownList - replace the loop with below:




/**

 * modified to include custome attributes for each checkbox item via the htmlOptions =>options property.

 * Works the same as in dropDownList control.

 */

foreach($data as $value=>$label)

{

	//make copy of the original $htmlOptions array

	$optionsCopy = array_merge($htmlOptions);

	///unset the option property from the copy so that it doesn't cause errors in other functions attempting to process the array

	unset ($optionsCopy['options']);

	$checked=!is_array($select) && !strcmp($value,$select) || is_array($select) && in_array($value,$select);

	$checkAll=$checkAll && $checked;

	$optionsCopy['value']=$value;

	$optionsCopy['id']=$baseID.'_'.$id++;

	///if there are any custom attributes set for this checkbox, set them on the copied array

	if ( isset ($htmlOptions['options'], $htmlOptions['options'][ $optionsCopy['value'] ]) ) {

		$custAttrs = $htmlOptions['options'][ $optionsCopy['value'] ];

		foreach ($custAttrs as $attrKey => $attrVal) {

			$optionsCopy[$attrKey] = $attrVal;

		}

	}

	$option=self::checkBox($name,$checked,$optionsCopy);

	$label=self::label($label,$optionsCopy['id'],$labelOptions);

	$items[]=strtr($template,array('{input}'=>$option,'{label}'=>$label));

}

unset ($htmlOptions['options'] );