Oct 18 2008

Unique validation rule in CakePHP 1.2

Category: CakePHP,PHPvbmendes @ 08:46

In this post I will show an example of how apply the unique validation rule in CakePHP 1.2. It’s very important to do in some cases, like saving users. You must make sure that the username is unique in your system. Lets see how it works:

var $validate = array(
    'username' => array(
        'rule' => array('isUnique'),
        'message' => 'Username already in use.'
    )
);

I wrote a custom function to validate it, but thanks to Dia now I know the built-in validation rule.

Tags: , , ,

9 Responses to “Unique validation rule in CakePHP 1.2”

  1. Dia says:

    CakePHP 1.2 has a built-in unique validation rule :

    var $validate = array( ‘myField’ => array( ‘rule’ => ‘isUnique’, ‘message’ => ‘data already exists’ ));

  2. vbmendes says:

    Thanks for the information, I didn’t see this rule. But I already fixed the post to use it. It’s better then custom functions.

  3. matheus says:

    it´s a super code!

    onde eu posso pegar a “tag cloud”? muito bem sacada…

    pode mandar pro email.

    até mais pessoal. congratulations!

  4. harry says:

    Cool thnks!! :-)

  5. Shaun says:

    But what if you need unique validation for multiple fields? For example, a comment rating system in which a user is allowed only one vote, so a unique key is set for user_id and comment_id.

  6. Boldis Media says:

    Thanks a lot! It’s very useful for version 1.2

  7. Rene Binet says:

    This is a good article, I discovered your blog page checking bing for a related topic and arrived to this. I couldnt get to much additional details on this article, so it was wonderful to locate this one. I will likely end up being returning to look at some other posts that you have another time.

  8. jobish says:

    Dear friend

    How can mange edit for username

  9. Unique validation rule in CakePHP 1.2 - dhansson - dhansson says:

    [...] Unique validation rule in CakePHP 1.2 Relaterade inlägg :jquery form validation with cakephp model validation10 Useful jQuery Form Validation Techniques and TutorialsMultiple Validation Sets in CakePHP 1.2 – Snook.ca#8958 (Optimize rewrite rule generation) – WordPress TracjQuery autocomplete Helper | The Bakery, Everything CakePHPBy Blogsdna [...]