CHtml::activeRadioButtonList not flexible enough?



echo CHTML::activeRadioButtonList($form, "options", array("1"=>"Boston", "2"=>"Newton", "3"=>"Other") );


It's output is



<input id="ytCommandForm_options" type="hidden" value="" name="CommandForm[options]" /><input id="CommandForm_options_0" value="1" type="radio" name="CommandForm[options]" /> <label for="CommandForm_options_0">Boston</label><br/>


<input id="CommandForm_options_1" value="2" type="radio" name="CommandForm[options]" /> <label for="CommandForm_options_1">Newton</label><br/>


<input id="CommandForm_options_2" value="3" type="radio" name="CommandForm[options]" /> <label for="CommandForm_options_2">Other</label>


However, I don't want to use <label> tag, I'd rather use <span>, How can I do this?

My solution is completely copy over the 3 functions from CHTML and made my own version, but it'll causing troubles when I upgrade, so I'd rather do it in a better way. Please suggest.

You should try to avoid copying over core classes in the Yii framework.

You should always subclass.



class MyHTML extends CHtml


{


    public static function activeRadioButtonList( ... )


    {


    }


}


Thanks, Jerry. I did my own MyHtml extends CHtml and override 3 functions.

The issue is: although in $htmlOptions there's template,seperator etc. , it's not flexible enough to support another tag.

I tried to use "template"=>"{input}<span>{label}</span>" but it still get a <label> tag in {label}.

Hope to get the developer's attention on this to improve it later.

Here's what I do to solve it






public static function myActiveRadioButtonList($model,$attribute,$data,$htmlOptions=array()){


                $result= parent::activeRadioButtonList($model,$attribute,$data,$htmlOptions);


                $result= str_replace("<label", "<span", $result);


                $result= str_replace("</label", "</span", $result);


                return $result;


        }


And I hope it can be done like this



echo CHtml::activeRadioButtonList($form, $option_name, array("1","2"), true),


             array("template"=>"{input}<span>{label}</span>",


             "separator"=>"&nbsp;&nbsp;&nbsp;"


                )


             );


Not sure why you want to use span instead of label… Using label allows you to click on it to select the corresponding radio.

Need to keep the 4 radio buttons in one line and label has a css of width: 100px

Why not change the css style for labels?

What I hope is the template in htmlOptions to be more flexible.

There's more than 3 ways to do that: CSS, str_replace, Table layout etc.

But the best way I think is by improving the template parameter so it'll be more flexible.

The problem with your proposal is that we would have trouble generating a label tag which requires "for" attribute to contain the target id.