Get Label From Related Model If Field In Default Model Is Required * (Solved)

I have already submitted this answer in another post, but I think lots of people are looking for something like this - so I repeat it here with its own heading:

labelEx works fine on the DEFAULT model’s attributes, but it does not get an attribute (normally the FK) label in the RELATED model.

This means you have to label your FK in all models where it is used. This obviously results in multiple labels in different models for basically the same field.

I don’t want this. I want to label a field in its own model, and never again in any other model.

So, test if the FK is ‘required’ in the default model; but then get its label in its own (related) model. To do this use “activeLabel”.

You can even use the label of another field if required e.g. in the following code I display the label of enterprise_name while I test if enterprise_id is ‘required’ (I do this because I have a dropdownlist of all the names, but the resulting ID is stored in the FK):


	echo CHtml::activeLabel($model,'relationToEnterpriseModel.enterprise_name', 	array(

		'required' => $model->isAttributeRequired('enterprise_id')