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.
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:
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?
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 */
array(
'required' => $model->isAttributeRequired('attribute_id') /* test against default model attribute 'required' rule */
)
);?>
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).