Labelex() Changing Text

Hi all,

I have a form which contains the following:




echo $form->labelEx($model, 'Amount(US$):');

echo $form->textField($model,'amount');

echo $form->error($model,'amount');



For some reason the label is being displayed as "Amount ( Us$). Does anyone know why this might be happening?

Thanks!

The second parameter to CActiveForm::labelEx() is the name of the model attribute. You should update the model’s attributeLabels() method to provide the correct label.

If you want to do it in the form instead, you can do so like this:




echo $form->labelEx($model, 'amount', array('label'=>'Amount(US$):'));



Ahh I see, thanks.

So is there any real need for a separate model attribute for the label? It’s only purpose is to provide a description for the corresponding text field.

labelEx by default uses the attribute label defined in the method ‘attributeLabels()’ of the model.

So you can assign the label there:


 

        public function attributeLabels()

	{

		return array(

			'amount' => 'Amount(US$):',

			...

		);

	}



So you don’t have to set the ‘label’ option:


echo $form->labelEx($model, 'amount'));

But with the label option you can override the label defined in attributeLabels().

I’m not sure if I fully understand, but by specifying the model attribute, Yii can add an error class to the label when it’s generated, so it can be styled differently. In general, the label is coloured red when an error occurs.

Thanks for the replies. So if I want to have the label text as (US$) and the "required" validation error text to be something different i.e. "USD cannot be blank", the only way to achieve this would be to change the label or override the validation error message?

I believe so.