labelEx not showing required '*' on related attribute

Hi Guys

If I use $form->labelEx($model, ‘attribute_id’) then labelEx displays the required ‘*’ after the label.

But, if I use $form->labelEx($model, ‘relation_name.attribute_name’) then the ‘*’ is not displayed, although attribute_name is made ‘required’ in the related model’s rules.

Any ideas?


It’s normal, Yii checks (see source code for CHtml::activeLabelEx) that the attribute is required within the model.

Maybe there could be a possible extension or enhancement, but I’d suggest a general workaround that would help for validation as well. Just pass on to the view an instance of the related model as well and use it directly:

$form->labelEx($relatedModel, 'attribute_name')

Thanx for the reply bennouna.

However, I hope we can find another solution without having to pass additional instances of all the related models we are tunneling to.

The related attribute’s Label is already available in $model. Is the problem that the related attribute’s Required status is not also available in $model?

How would you deal with related model validation if you don’t pass instances of related models?

On a side note, an uglier solution imho is to use label instead of labelEx and manually check the required attribute with isAttributeRequired:

echo CHtml::label('label', 'forInputId', array('required' => $model->relation->isAttributeRequired('attribute')));

But again, how would you validate the related model, I’m curious?

Ya, I see what you mean.

So I guess I have to decide what ugly option to use: label or extra models.

Thanx for your input.

or extend CHtml to override activeLabelEx…

I used CHtml::activeLabel instead of CHtml::label because it looks like label inserts a line break.

<?php echo CHtml::activeLabel($model,'relation.attribute_name', /* use related model attribute label */


		'required' => $model->isAttributeRequired('attribute_id') /* test against default model attribute 'required' rule */



Nice workaround.

For validation, I’ll be grateful if you update the thread when you deal with validation (I’ve tried some scenarios and will be glad to learn one more possibility for related models).