custom form error messages with HTML


I’d like to add some HMTL around the form error messages (I’d like to display an icon in front of the message)

so far I am doing like that:

public function rules()


		$message ="<span class='ui-icon ui-icon-alert'></span><span class='message'>". Yii::t('yii','{attribute} cannot be blank.')."</span>";

		return array(

			array('firstname, lastname, email, password, country, timezone, publicName', 'required', 'message'=>$message), ...

it does work, but I feel it is not very clean…

what do you guys think?

thanks a lot

you are right, maybe you just did little spagette :stuck_out_tongue: (i don’t know how to spell it) in MVC artiticture.

mixing model and view is not a good idea, instead you can put that HTML code inside your related view file, the error message is just a plain text and you view it the way you like.

good luck :slight_smile:

thanks for your reply Muaid (and sorry for the delay)

yes, you are right…

I am not sure how to do this…

how can I insert HTML code in the DIV that is generated via $from->error(… ?

I saw something called CClipWidget, it seems I could use this, couldn’t I? still not sure how to do it thu :)

it is simpler that you think :) and CClipWidget have no rezone to be here.

let us create (for example) a new Yii project.

open the file: protected\views\site\contact.php

and you will see each error appear there like this

<?php echo $form->error($model,'email'); ?>

you can simply change it to :

<span class='ui-icon ui-icon-alert'></span>

<span class='message'>

  <?php echo $form->error($model,'email'); ?>


important NOTE: you don’t have to set a message manually for each validator you set unless you want to change the default one !!

i mean this line :

Yii::t('yii','{attribute} cannot be blank.')

will show by default for each ‘required’ field you set in rules(

as a result your previous rules() function in model will be:

public function rules()


  return array(

    array('firstname, lastname, email, password, country, timezone, publicName', 'required'),


that is it :)

don’t hesitate to ask any further question.

ok, thanks a lot for your help!

However, I fear that in this case this icon will be displayed all the time, and I want it to appear at the same time as the message.

(like the attached file)

thanks again Muaid

sorry for late,

you are right this is not a prober way to solve the problem.

i checked CActiveForm->error() function and i think there is no direct way (configurations) to make the error inside SPAN because DIV tag is fixed and this function will call CHtml::tag() function to generate html output with DIV tag.

so you have to make your own way on this (you can extend Yii for new widget or CActiveForm if you like) or you can simply change your CSS code to achieve this.


the CActiveForm needs to be modified:



                //$option['clientValidation']="js:function(value, messages, attribute) {\n".implode("\n",$validators)."\n}";

                $string = str_replace('messages.push("','messages.push("<span class=\'ui-icon ui-icon-alert\'></span><span class=\'jr-message\'>',implode("\n",$validators)) . "\n";

                $string = str_replace('");', '</span>");', $string);

                $option['clientValidation']="js:function(value, messages, attribute) {\n".$string."\n}";


I didn’t find the solution myself a Yii developer helped me out on this one (btw, I am just a web designer).

thanks Muaid for your help, it helped us going on the good way!