dsa1230000 2016-02-16 12:19
浏览 26
已采纳

cakephp模型中的电子邮件验证

I have the below setup of validation rules. For some reason, 'on' => 'create' block doesn't work. The conditions to be implemented are standard create / modify regarding email. Also, in edit section, I'm getting the error from 'on' => 'create' block.

How to validate the email? I'm using 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.

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)){
                     }
                 }
             }
        }
  • 写回答

2条回答 默认 最新

  • dttnb997315 2016-02-16 12:53
    关注

    I think you are misunderstanding what Cake's isUnique rule checks for and as a result over complicating things. Cake defines isUnique as:-

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历