Tag Archives: make field unique

Cakephp3 make a field unique

23666To do this, simple open your table model file and add the following;

public function validationDefault(Validator $validator)
{

//the below code makes the field "title" validate as unique
$validator
->requirePresence('title', 'create')
->add('title', 'unique', ['rule' => 'validateUnique', 'provider' => 'table','message'=>'Error: Title already exists.'])
->notEmpty('title');

return $validator;
}

 Manual:
http://book.cakephp.org/3.0/en/core-libraries/validation.html