Search code examples
cakephpcakephp-3.xcakephp-3.7

CakePHP validateUnique not found


Created a UsersTable.php file by baking from my database.

I have the user name working as an email address just fine.

But then I wanted to ensure that new users weren't trying to create an account with an existing email address. I found this pretty simple validator method in the docs, which looks to be exactly what I need.

    $validator
        ->email('email')
        ->requirePresence('email', 'create')
        ->allowEmptyString('email', false)
        ->validateUnique('email', true);

However... I get:

Call to undefined method Cake\Validation\Validator::validateUnique()

Solution

  • validateUnique is a method on the Table object, not the Validator object. You need to use the Validator::add method as shown in the examples on the page to which you linked.