在cakephp中有条件地检查验证

在cakephp中有条件地检查验证,php,cakephp,Php,Cakephp,我的项目中有一个用户经理。当我添加用户时,必须调用所有验证,但根据我的要求,当我从用户发送电子邮件和密码登录时,我希望不调用电子邮件地址的验证 UserController.php public function login() { $this->layout = 'login'; if ($this->request->is('post')) { /** these conditions are used to call

我的项目中有一个用户经理。当我添加用户时,必须调用所有验证,但根据我的要求,当我从用户发送电子邮件和密码登录时,我希望不调用电子邮件地址的验证

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.'
        )
);

但是,当我登录时,它会显示“此电子邮件id已存在”。正确电子邮件地址的消息,但如果数据库中存在电子邮件id,我想跳过此验证。

您可以使用
beforeValidate()
()回调,该回调在每次验证之前都会触发,并混合一些您将在登录方法中设置的标志

因此,在模型的“添加标志”字段中:

protected $skipEmailUniqueValidation = false;
然后为其添加一些setter:

public function setSkipEmailUniqueValidation($value = true) {
    $this->skipEmailUniqueValidation = $value;
}
并添加
beforeValide()
方法:

public beforeValidate(array $options = array()) {
    if ($this->skipEmailUniqueValidation) {
        unset($this->validate['email']['isUnique']);
    }
    return parent::beforeValidate($options);
}
最后一件事是通过以下方式在操作中设置
skipmailuniquevalidation

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'));
        }
    }
}