The method CActiveForm::labelEx is a wrapper around CHtml::activeLabelEx which inturn a wrapper around CHtml::labelEx.
If you look at the code of CHtml::labelEx, if you set ‘label’ as false in htmloptions, the function returns empty without proceeding to render any html element in the form.
public static function activeLabel($model,$attribute,$htmlOptions=array())
{
if(isset($htmlOptions['for']))
{
$for=$htmlOptions['for'];
unset($htmlOptions['for']);
}
else
$for=self::getIdByName(self::resolveName($model,$attribute));
if(isset($htmlOptions['label']))
{
if(($label=$htmlOptions['label'])===false)
return '';//Here the function returns empty string.Nothing is rendered.
unset($htmlOptions['label']);
}
else
$label=$model->getAttributeLabel($attribute);//If $label is false, the function proceeds and renders empty string along with asterisk.
if($model->hasErrors($attribute))
self::addErrorCss($htmlOptions);
return self::label($label,$for,$htmlOptions);
}
The follwing code ensures that if we set attributeLabelfalse for any attribute, nothing is rendered(including asterisk).
‘label’ htmlOption of CHtml::label accepts boolean value of false, and it works as a switch to prevent the label tag from rendering.
But in the ‘id’-‘label’ pairs for CModel::getAttributeLabel, ‘label’ is always assumed to be a string. A boolean value of false is not appropriate for it, and it will not prevent the rendering of the label tag.
But that means IT IS a bug, somewhat. If documentation says that setting label to false in both cases (using ‘label’ in array and inside [size=“2”]attributeLabels() function) and in on of these cases it does not works as supposed, then either it is a bug in code or misleading information in documentation. Correct me, if I’m wrong.[/size]
I should have said : ‘label’ htmlOptions of CHtml::activeLabel accepts …
We generally assume that a label is a string. Considering the general understanding of the word ‘label’, it should be a string unless explicitly stated otherwise.
And we use the labels of the model attributes in many places. In error messages, in grid views, in detail views, … etc. The usage of the attribute label is not limited to the label tag. And in most of the cases we are not ready to handle an attribute label that can be ‘false’. The attribute labels should not be overloaded with an extra syntax that is only applicable when used in CHtml::activeLabel.
[UPDATE]
So I think it is not a bug, at least in the code. And I agree that ‘label’ htmlOptions of CHtml::activeLabel is a little confusing.
I take this problem, not as deep-coding understanding but as simple frontend user. In my opinion if two nearly exactly the same aproaches provides different output, then we’re talking about either bug in implementation or a misleading documentation, which is stating that two things will produce the same result, while it isn’t.
Additional question is, what is the use of label without text but with required attributte added, as second of my approaches now produces? For me this is a little bit useless. I would understand (and highly apperciate) any way of shoing label for required field without required attribute added, but I don’t see any use for exactly opposite situation – label without name but with required attribute.
While the both of them are equally called ‘label’, but the label for a model attribute and the html label tag are not the same thing. They are in the different layers.
I agree. It’s no use. You should avoid that kind of ugly output.
But, as far as I understand, you can’t blame it on Yii’s functional design and coding.