Missing colon in field’s labels

By default, Yii renders all forms with field’s labels not containing colon after label. This may be unwanted effect for some. The easiest way would be to change field’s label to contain that missing colon. But, that would produce an even more unwanted side-effect of having that colon included (as part of field label) in error message, when validation fails. Here is my simple workaround for this problem.

It goes like this. Declare “short” names (i.e. without colon) in model. They will be used for error messages. Then declare “full” names (i.e. with colon) directly in view, using CActiveForm::labelEx() method and an array as third parameter of its call.

So, in your model put attributes’ labels without colon:

[code language=”php”]
public function attributeLabels()
return array
‘name’=>’Full name’,
’email’=>’Email address’,
‘verifyCode’=>’CAPTCHA code’,

While in your view use attributes’ labels with colon:

[code language=”php”]
<div class="row">
<?php echo $form->labelEx($model, ‘name’, array(‘label’=>’Full name:’)); ?>
<?php echo $form->textField($model, ‘name’, array(‘size’=>70, ‘maxlength’=>70)); ?>
<?php echo $form->error($model, ‘name’); ?>

And that is all, that you need to do. See for yourself. Go ahead and test my trick.

Leave a Reply