Custom error message with {value} placeholder


I have a custom error message for validating the email input field:

array('email', 'email', 'message' => '{value} is not a valid email.'),

The output on the rendered form is:

{value} is not a valid email.

Is it simply not possible to have the {value} placeholder for email validation? Or am I doing something else wrong?

Also, is there a known workaround to make this placeholder work?

Thanks a lot,


1 Like

array('email', 'email', 'message' => "$this->email is not a valid email."),

I did not try it

but that’s what I do when creating a rule function that returns the value of the form,using the $ this-> fieldName

hopefully help you

see CValidate::message

array(‘email’, ‘email’, ‘message’ => “{attribute} is not a valid email.”),

use {attribute} as placeholder :lol:

Yii has only default build-in {attribute} placeholder mentioned by yiqing95.

If you want to have {value} placeholder too, you’ll need:

  1. Create into components folder your new validator similar to CEmailValidator:

class MyEmailValidator extends CEmailValidator


	public function clientValidateAttribute($object,$attribute)


		$message=$this->message!==null ? $this->message : Yii::t('yii','{attribute} is not a valid email address.');

		$message=strtr($message, array(






			$condition.=" && !value.match({$this->fullPattern})";

		return "

if(".($this->allowEmpty ? "$.trim(value)!='' && " : '').$condition.") {





	protected function addError($object,$attribute,$message,$params=array())







  1. Add validation rule using your validator:

array('email', 'MyEmailValidator', 'message' => '{value} is not a valid email.'),

That’s should work.

Thanks a lot for your replies.

Either Weavora Team’s or MR D’s solution is working fine. And yes, yiqing95’s solution as well - if you want to use the {attribute}-placeholder.

Thanks a lot! :)

I have a problem with the custom error message…

Here is my situation.

I am calling data from the ‘turnover’ table in my _form as such:

ID | Name

1 |

2 | R0m - R1.5m

3 | R1.5m - R2.5m

4 | R2.5m - R3m

and placing the ID value into my business table.

i.e. the Names from the turnover database are shown to the user, but the ID’s from the turnover database are stored in the business database. Here is the code below for those that need this same functionality… (as found in my _form view)

<div class="row">

	<?php $turnover = BizTurnover::Model()->findAll(); ?>

	<?php echo $form->labelEx($model,'turnover'); ?>

	<?php echo $form->dropDownList($model,'turnover', CHtml::listData($turnover, 'turnover_id', 'name')); ?>

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


<br class="Apple-interchange-newline">

In my business model I need the validator to activate (show an error that he has NOT selected a value) when the user does not make a selection… but the default selection is ID 1… which shows as blank… but is actually an ID of 1!.. because it has an ID, the validator says that there is a value, and therefore no error is shown, so I’ve decided as a work-around, I’ll let the validator use the min/max feature, but obviously the error that comes up is not really correct for the user. So to change the error I created a custom error…

array('turnover', 'numerical', 'min'=>2, 'message'=>'Please select a value.'),

The problem now is that the custom error does not display, it never changes no matter what I do!

Am I doing this whole process incorrectly? Or am I doing something wrong?

Kind Regards,


Hi G.

Could you explain why you have a blank Name in your db for the ID 1?

If your goal is to provide a blank entry in your view’s dropdown, it’s not the best way to do it. If not, you can skip reading :)

So it that is your goal, you’d better do it like this:

<div class="row">

        <?php $turnover = BizTurnover::Model()->findAll(); ?>

        <?php echo $form->labelEx($model,'turnover'); ?>

        <?php echo $form->dropDownList($model,'turnover', CHtml::listData($turnover, 'turnover_id', 'name'), array('empty' => 'Please select a value.')); ?>

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


And in your model rule:

array('turnover', 'required', 'message'=>'Please select a value.'),

But that means of course getting rid of the empty Name record in your table (that is ID 1) :)

Yes, that was the reason for my “blank” id 1… and it has now been removed :)

THANK YOU SO MUCH, this works perfectly!

Is there perhaps a way of "forcing" the dropdown box to select the empty value… like a default selection… as when I go to the form, random values are put into the dropdown box (sometimes due to prior selections, but this not always the case), so should the client NOT choose something, he will suddenly have a turnover which is incorrectly put there.

Sorry about the double posting… won’t happen again


You’re welcome.

Normally the syntax I used above automatically selects the ‘empty’ prompt[color="#FF0000"]*[/color]. Just Try it and get back to us if it’s not the case :)

[color="#FF0000"]*[/color]: it selects the ‘empty’ prompt unless you’re in update scenario, in which case it selects the attribute’s value corresponding entry.


Talk about stupid mistakes!

Yes, I realised now that I am in update mode, and therefore it’s correct that it must select the the field that’s corresponding to the DB. I changed the DB values, and all working 100%.

Thanks, you are a lifesaver!