简体   繁体   English

cakephp模型中的电子邮件验证

[英]Email Validation in cakephp Model

I have the below setup of validation rules. 我具有以下验证规则设置。 For some reason, 'on' => 'create' block doesn't work. 由于某些原因, 'on' => 'create'块不起作用。 The conditions to be implemented are standard create / modify regarding email. 要实施的条件是有关电子邮件的标准创建/修改。 Also, in edit section, I'm getting the error from 'on' => 'create' block. 另外,在编辑部分,我从'on' => 'create'块得到了错误。

How to validate the email? 如何验证电子邮件? I'm using CakePHP v 2.6.1. 我正在使用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;
    }

}

Also, I'm not getting the $this->data[$this->alias]['id'] value. 另外,我没有得到$this->data[$this->alias]['id']值。

My Controller has the following section: 我的控制器有以下部分:

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)){
                     }
                 }
             }
        }

I think you are misunderstanding what Cake's isUnique rule checks for and as a result over complicating things. 我认为您误会了Cake的isUnique规则要检查的内容,并因此而使事情复杂化。 Cake defines isUnique as:- Cake isUnique定义为:

The data for the field must be unique, it cannot be used by any other rows 该字段的数据必须唯一,其他任何行都不能使用

When it checks if a value is unique it is smart enough to exclude existing data of the current row (which appears to be what you are attempting to do with your editunique rule). 当它检查一个值是否唯一时,它足够聪明,可以排除当前行的现有数据(这似乎是您尝试使用editunique规则进行的操作)。

So you just need your validation rules to look like:- 因此,您只需要您的验证规则即可:

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

This removes the editunique rule and drops the on condition of your unique rule. 这将删除editunique规则,并删除unique规则的on条件。

As of cakephp 3.0 in the entities table it should look something like this 从实体表中的cakephp 3.0开始,它应该看起来像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM