cakephpadditionregistering

search by user.email before get it save cakephp


this is my public add function, but there is a little mistake,

that if the user (his email or username) are exist, this function will save it anyway !

so, how can i do query to search by the [user][email] and then search by the [user][username] to confirm that the username and email which will go to database is a new data with username and email

 public function add() {
    if ($this->request->is('post')) {
       ////here it should be the query !
        $this->User->create();
        if ($this->User->save($this->request->data)) {
    $this->redirect(array('controller' => 'users', 'action' => 'login' ));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('الموقع مش عارف يسيفك يا فقيع, حاول تاني'));
        }
    }
}

Solution

  • Add validation in your User model

    public $validate = array(
        'username' => array(
            'emailEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Sorry, email cannot be empty',
                'last' => true,
            ),
            'email' => array(
                'rule' => array('email'),
                'message' => 'Must be a valid email',
                'last' => true,
            ),
            'emailExists' => array(
                'rule' => 'isUnique',
                'message' => 'Sorry, this email address is already in use',
            ),
        ),
    
    );