cakephp 模型中的电子邮件验证

Email Validation in cakephp Model

我有以下验证规则设置。由于某种原因,'on' => 'create' 块不起作用。要实施的条件是关于电子邮件的标准创建/修改。另外,在编辑部分,我收到来自 'on' => 'create' 块的错误。

如何验证邮箱?我正在使用 CakePHP v 2.6.1。

public $validate = array(
    'email' => array(
        'required' => array(
            'rule' => array('email'),
            'message' => 'Kindly provide your email for verification.'
        ),
        'maxLength' => array(
            'rule' => array('maxLength', 255),
            'message' => 'Email cannot be more than 255 characters.'
        ),
        'editunique' => array(
            'rule' => array('editunique'),
            'message' => 'Provided Email address already exists.',
            'on' => 'update'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'Provided Email already exists.',
            'on' => 'create'
        )
    )
);


public function editunique($email) {
    // email should be one and of the logged in user only.
    if ($this->find('count', array(
        'conditions' => array(
            $this->alias . '.id <>' => $this->data[$this->alias]['id'],
            $this->alias . '.email' => $email
        )
    )) > 1) {
        return false;
    }

}

此外,我没有得到 $this->data[$this->alias]['id'] 值。

我的控制器有以下部分:

if ($this->Client->hasAny(array('Client.id' => base64_decode(trim($this->request->query['client_id']))))){
            if ( $this->request->is('ajax') && $this->request->is('post') ){
                $this->Client->create();
                $this->Client->id = base64_decode(trim($this->request->query['client_id']));
                $this->Client->set($this->request->data);
                // validate
                if($this->Client->validates()) {
                    // save the data after validation
                    if($this->Client->save($this->request->data)){
                     }
                 }
             }
        }

我认为您误解了 Cake 的 isUnique 规则检查的内容,结果使事情变得复杂。蛋糕 defines isUnique 为:-

The data for the field must be unique, it cannot be used by any other rows

当它检查一个值是否唯一时,它会足够聪明地排除当前行的现有数据(这似乎是您尝试使用 editunique 规则执行的操作)。

所以您只需要您的验证规则如下所示:-

public $validate = array(
    'email' => array(
        'required' => array(
            'rule' => array('email'),
            'message' => 'Kindly provide your email for verification.'
        ),
        'maxLength' => array(
            'rule' => array('maxLength', 255),
            'message' => 'Email cannot be more than 255 characters.'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'Provided Email already exists.'
        )
    )
);

这将删除 editunique 规则并删除 unique 规则的 on 条件。

从实体中的 cakephp 3.0 开始,table 它应该看起来像这样

   namespace App\Model\Table;

   public function validationDefault($validator)
   {
    $validator
        ->email('email')
        ->add('email', 'email', [
            'rule' => [$this, 'isUnique'],
            'message' => __('Email already registered')
        ])
        ->requirePresence('email', 'create')
        ->notEmpty('email', 'Email is Required', function( $context ){
            if(isset($context['data']['role_id']) && $context['data']['role_id'] != 4){
                return true;
            }
            return false;
        });
    return $validator;
}
}


function isUnique($email){
    $user = $this->find('all')
        ->where([
                'Users.email' => $email,
          ])
        ->first();
        if($user){
            return false;
        }
        return true;
}