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.
mikl
(Mike)
April 3, 2010, 9:01am
2
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>
jerry2801
(Jerry2801)
April 3, 2010, 4:32pm
3
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~
jayrulez
(Waprave)
April 3, 2010, 6:15pm
4
You should still specify the for attribute. Good practice.
Maybe we can have CHtml::labeledCheckbox(), CHtml::activeLabeledCheckbox()
and CHtml::labeledRadioButton(), CHtml::activeLabeledRadioButton().
mikl
(Mike)
April 4, 2010, 3:05pm
5
A fourth argument $omitEndTag=false to CHtml::activeLabel() might be more flexible. If true, no end tag would be rendered.
onic
(Orf)
May 30, 2012, 7:30am
6
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