Unique validator - ignore current value on update


I have a field which has to be unique, but the user is allowed to change it. So when I update, it needs to ignore its current value when evaluating if it is unique.

How to do this ?

Have you tried the unique validator?

Yes, that is what I’m using to check the uniqueness of the data. Works fine on insert, but how to do this on update ?

Seems strange because the unique validator does check if the record is new and if it is not it ignores the current value…

are you using Yii 1.1.5?

can you post your validation rule and form code?

My bad, had a mix up in my rule’s scenario setting. Works fine now, thanks.

Hi, would you be able to post your validation rules? I’m clearly being stupid but am having trouble with the whole don’t-include-this-record-if-no-update-unique-validator thing.


If you have a table with a field "username", and you want it to be unique… this is the validation rule for this:

array('username','unique','message'=>'{attribute}:{value} already exists, please choose a different one.'),

You can even avoid to specify the message:


This one works fine too.

Thanks for the replies. :slight_smile:

Okay, I get that, but the problem is that I’m loading the user’s details into the form (i.e. pre-populating it with their current data, and then letting them hit submit to change it). In this scenario validation fails because the value that’s in the form is the same as the one that’s in the database. Validation is quite right to fail in these circumstances.

I think I want to disable validation if they hit submit without having changed the value. Or I need to ignore Yii’s validation stuff altogether and do it myself?

Maybe I’m thinking about it wrong, but the flow of the app kinda means it has to work like this; I’m using Facebook and Twitter external authentication, so the first thing the user is prompted to do when they log in is select a username (I auto-generate one based on the Facebook ID) and it’d be nice to load the current one into the form so they can just edit it and hit submit.

Edit: I found another forum topic on it, but I can’t embed links since this is one of my first posts. The topic is “uniqueValidator on update”, but it’s in the bug reports forum, and I don’t think what I’m trying to do can be a bug.

You are talking about updating existing records not creating new records…

The unique validator takes that into account… if I remember right… I replyed to that topic you are refering… (btw, you could post links after your 3 post)

Yeah, it’s just on update that I need the validator not to check its own record. Create is fine and I need it to kick in then, obviously.

Can’t get the unique validator to recognise that it’s an existing record. I figure I must be doing it wrong because this is a pretty regular task.

Thanks again for your help.

Check the source - http://code.google.com/p/yii/source/browse/tags/1.1.6/framework/validators/CUniqueValidator.php#106

It does take in consideration the old filed value…

So it can be that you are using an old version of Yii or something else… in that case you need to post here your validation rules… and actionUpdate/view code

I’m using v1.1.6 and have exactly the same problem. It should always check whether the value is unique except for the case when you load an update form and the user doesn’t change the email address. I guess the workaround would be to create a new field and set scenarios similar to here: http://www.yiiframework.com/forum/index.php?/topic/12229-best-way-to-implement-implicit-change-of-password-solved/ . But is that really necessary?

Here is the code that doesn’t work and before implementing the work around.


array('email', 'unique'),


	<div class="row">

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

		<?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>100)); ?>

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