I want to check if an email/username is unique from a controller. I wrote model validation for isUnique and it works. But I also want a way to use it in controller. Some thing like
$this->User->validates(array('email'=>'someemail@domain.com'));
or
$this->User->checkunique();
//is this the right way to write a different function again.
I know I can use findByEmail from controller and check if it returns empty result set. But I feel that there is a simpler approach or already available way cakephp offers. Please correct me if I am wrong and let me point out the correct approach in this context.
Thanks,
kiran
Have you had a look at the Cook Book already? There’s a nice explanation on how to do it:
http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html
To sum it up. First set the data to validate:
Then validate it and pass a list of fields to restrict validation to if neccessary:
Whether you wrap this up in a custom model method is totally up to you, though from a DRY point of view it’s a good idea to do so.