Adding A Css Class To Specific Items Of Yii Active Checkboxlist

Working with Yii and active checkboxlist. I know the params. I need to add a flag css class to the items. This is my code:


$form->checkBoxList($model, 'items', $selected, array(

    'class'=>'default_class'

));

This code just adds a default_class to every item. But I need a different class for specific items (conditional).

The only way you are going to get what you want here is too implement a custom algorithm, the checkBoxList does support a template option but it has no conditional support (you’d need to be able to pass in a callback or similar).

I know it. What I don’t know is HOW?!

I’m only trying to help.

Anyway.

It really depends if you want something re-usable or a one off edge case. I can point you in the right direction but I cannot write your software for you.

Something one off could work like this:




foreach ($models as $model) {

  echo '<input type="checkbox" name="' . CHtml::activeName($model, 'attribute') . '[]" value="' . $model->valueField . '" ' . condition ? 'class= "your-class" : '' . '/>';

}



Refactor or massage to suit.

This one helps. Thanks.