Multiple Label Translations For Model

Not sure if I’m missing something here.

What I have is a CActiveRecord model for a table in my database called ‘EmployeeDetails’. This model is used in 2 places in my application, the ‘Create New Employee’ form and the ‘Update Existing Employee’ form, and I would like the label translations to be specific to which view the model is being used in. For example when creating a new employee the ‘First Name’ label should be “Enter First Name” and when updating an existing employee it should be “Type New Name”. Is this possible, maybe using the scope of the model? Surely I do not have to create 2 CActiveRecord classes to handle the different labels…

Thanks for any input.

Hi rtlm,

You can not have multiple labels for an attribute of a model.

Consider using CHtml::label directly instead of CActiveForm::label or CHtml::activeLabel.




/* echo $form->label($model, "first_name"); */

$label = "Enter " . CHtml::activeLabel($model, "first_name");

$inputName = CHtml::resolveName($model, "first_name");

$for = CHtml::getIdByName($inputName);

echo CHtml::label($label, $for);



http://www.yiiframework.com/doc/api/1.1/CHtml#activeLabel-detail

(Click "Show" link to see the source code and examine what it is doing to generate the label tag.)

Thanks softark for the reply. I had a thought which I’d like to run by you. From my understanding (still new to Yii so not fantastic) I should be able to alter CActiveRecord’s method ‘attributeLabels()’ to check the scenario, then return a different array based on what the scenario currently is. So if the model scenario is “Create”, then the array will return the “Create” labels, and the “Edit” scenario the “Edit” ones. This would allow me to translate the labels with Yii::t() as well.

Does this sound like it would work, and if so, would it be advisable to do so, or could there potentially be knock-ons?

Thanks!

Well, it looks to me that “Enter First Name” is not a label but a prompt. You’d better not include an instruction message in a label. Note that the label is used for error messages, too. You don’t want a message like “Enter First Name must be 20 characters or less.”, do you?

[EDIT]

I think the idea of changing labels according to the scenario should work. But I wouldn’t do so.

hmmmm,

I am starting to think I may have a design flaw in my application. In my situation I have one model that needs to be updated in multiple places, and depending on the ‘scenario’ have different labels. Would it be better to create a CFormModel instance for each one, then on the controller handle the setting of the underlying model? That way each form can handle its own labels.

I initially had it set up like this, but changed it as I was getting a lot of code duplication particularly in the validation of the fields.

As for the labels being more like prompts, yes that’s what I really need. In this case should I just lose the label all together and output the text directly?

The way I got around this is by manually setting the error message to what I need, rather than using the default.

Again, this is starting to make me feel like there is a design flaw here… :o

Yes, I would rather do so than make things complicated.

I love the word “KISS”, which doesn’t stand for “Keep It Simple, Stupid” but for “Keep It Simple and Stupid”. IMO it has helped me a lot more than the word “DRY” that tends to lead you into an unnecessarily complicated design.