1
votes

I've a user manager in my project. When i'm adding user all validations must be called but according to my requirement when i'm email & password from user to login then i want that isunique validation for email address not be called.

UserController.php

public function login() {
        $this->layout = 'login';
        if ($this->request->is('post')) {
            /** these conditions are used to call validations forcefully **/
            if (!empty($this->request->data)) {
                $this->User->set($this->request->data);
                if($this->User->validates()) {
                }
            } else {
                $errors = $this->User->invalidFields();
            }
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                //$errors = "Invalid email or password, try again";
                $this->Session->setFlash(__('Invalid email or password, try again'));
            }
        }
    }

User.php

public $validate = array(
'email' => array(
            'required' => array(
                'rule' => 'notEmpty',
                'message' => 'A email is required.'
            ),
            'email' => array(
                'rule' => array('email', true),
                'message' => 'Please fill a valid email address.'
            ),
            'isUnique' => array(
                'rule' => array('isUnique'),
                'message' => 'This email id already exist.'
            )
        ),
        'password' => array(
            'rule' => 'notEmpty',
            'message' => 'Please enter password.'
        )
);

But when i login it shows "This email id already exist." message for right email address but i want to skip this validation if email id is exist in database.

1

1 Answers

1
votes

You can use beforeValidate() (manual) callback which is triggered each time before validation mixed with some kind of flag which you will be set in login method.

So in your model add flag field:

protected $skipEmailUniqueValidation = false;

Then add some setter for it:

public function setSkipEmailUniqueValidation($value = true) {
    $this->skipEmailUniqueValidation = $value;
}

And add beforeValide() method:

public beforeValidate(array $options = array()) {
    if ($this->skipEmailUniqueValidation) {
        unset($this->validate['email']['isUnique']);
    }
    return parent::beforeValidate($options);
}

Last thing to do is setting skipEmailUniqueValidation in your action by:

public function login() {
    $this->layout = 'login';
    if ($this->request->is('post')) {
        /** these conditions are used to call validations forcefully **/
        if (!empty($this->request->data)) {
            $this->User->set($this->request->data);
            $this->User->setSkipEmailUniqueValidation(true); // here
            if($this->User->validates()) {
            }
        } else {
            $errors = $this->User->invalidFields();
        }
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            //$errors = "Invalid email or password, try again";
            $this->Session->setFlash(__('Invalid email or password, try again'));
        }
    }
}