CHtml helpers - enclosing <label>

Having the CFormModel $form, in the view I have to use CHtml like this:


$attr = 'rememberMe';

echo '<label for="',CHtml::getIdByName(CHtml::resolveName($form,$attr)),'">';

echo CHtml::activeCheckBox($form,'rememberMe');

echo $form->getAttributeLabel('rememberMe');

echo '</label>';



in order to get an enclosing <label> around the checkbox. Using CHtml::activeLabel() is not what I want because of the visual nastiness of the browser, which renders a line break between the actual checkbox and its label.

Is there any other, cleaner way, of achieving it? Without temporary variables like $attr, which I am forced to use, as CHtml::resolveName() has a reference to its second parameter.

Didn’t even know, that you can do that, so thanks for pointing out :) .

From what i’ve read, if you enclose your input tag inside the <label> you don’t need the for attribute anymore. So how about:


<label>

  <?php echo CHtml::activeCheckBox($form,'rememberMe') . $form->getAttributeLabel('rememberMe'); ?>

</label>

this can not support ie browser~

You should still specify the for attribute. Good practice.

Maybe we can have CHtml::labeledCheckbox(), CHtml::activeLabeledCheckbox()

and CHtml::labeledRadioButton(), CHtml::activeLabeledRadioButton().

A fourth argument $omitEndTag=false to CHtml::activeLabel() might be more flexible. If true, no end tag would be rendered.

Just add the following code to your CSS:




input[type="checkbox"] {

	float: left;

}



In Milestone 1.1.11 it should be possible to wrap a lable around an input:

Checkout Issue #373 on Github