Activelabelex Without Label?

I just noticed something that seems to me a little bit strange.

If you set a field label to FALSE like this (directly in form view):


<?php echo $form->labelEx($model, 'username', array('label'=>FALSE)); ?>

no label at all will be rendered, as documentation says.

But, if you try to achieve the same, using attributeLabels() in form model:


public function attributeLabels()

{

    return array

    (

        'username'=>FALSE

    );

}

You’ll see that label isn’t rendered, but required character (asterisk by default) is rendered.

Isn’t that some kind of bug or at least in-consistence?

Dear Trejder

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 attributeLabel false for any attribute, nothing is rendered(including asterisk).




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 '';

			unset($htmlOptions['label']);

		}

		else

                {

			$label=$model->getAttributeLabel($attribute);

                        if($label==false) return ''; //Not going to proceed further.

                }

		if($model->hasErrors($attribute))

			self::addErrorCss($htmlOptions);

		return self::label($label,$for,$htmlOptions);

	}



Dear seenivasan,

You’re showing me a piece of Yii core code and trying to convince me that I did not see what I saw, right?

I know the code of activeLabelEx and others. But, please take a look at attached screenshot.

3314

activeLabelEx.png

I don’t know, how it is possible? I’m just saying, what I saw in nearly-fresh Yii app (so really small chances that I did messed up something).

Dear Trejder

I am just posting what I am knowing.Nothing more than that.

In our case this is what finally rendered.




<label>content</label>



The following is the content




$content=self::$beforeRequiredLabel.$label.self::$afterRequiredLabel;



By default




self::$beforeRequiredLabel='';



In ourcase




$label=false;



By Default




self::$afterRequiredLabel=' <span class="required">*</span>';



If we echo "self::$beforeRequiredLabel.$label.self::$afterRequiredLabel", What we are going to get is "*"

Regards.

‘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.

It’s so-called “by design”.

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]

Sorry. A correction is needed.

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.

Thank you for your detailed explanation.

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.

Well, I don’t think so.

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.