Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely
outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult
Google or our search first if you want to get an answer ; ).

Imagine that you want to make sure that a username that is already recorded in the database is not used again. Such a feature is often used for login systems, where both email and user names are tracked down and refused, if there is already a user in the database with that name or email.

With this handy function in this can get very easy. Simply copy the source code to your AppModel class, which is found in your /cake/ folder. Then move on to the next steps to make this work.

You need to pass the form field name, its value and an optional id to it, with which you can exclude a given database row from the search. This is important to make editing work. Say for example the user with name "Peter" changes his email. If you preserve the name "Peter" in the username form field, using this function without passing it the id of Peter's row would not work. It would always reject taking on Peter's name, as there is already one "Peter" in the database.

What does your view look like?

You have to provide a tag error message for the form field with the form field's name prefixed by 'unique_':