【问题标题】:Skipping validation for only one column on a form仅对表单上的一列跳过验证
【发布时间】:2014-07-22 18:17:41
【问题描述】:

我的 Cake2.4 应用程序有一个密码重置表单,这给我带来了一些验证问题。

当我尝试对用户进行简单保存时,我遇到了一个验证错误,即电子邮件地址不是唯一的——即使我只是在更新用户的密码。

当我在我的保存中将$validate 设置为 false 时,表单可以正常工作。但是,这会跳过所有验证规则,包括我的密码验证规则。

如何进行此保存,以便它不会抱怨电子邮件地址不唯一,并且仍能正确验证密码?

电子邮件地址是唯一的,据我所知,我的控制器操作并未尝试更改它。在我的模型中规定“创建时”将无济于事 - 否则用户可以将他们的电子邮件地址设置为其他人的。

public function reset_password($token = null) {

    if ($this->Session->read('Auth.User')) {
        $this->Session->setFlash(__('You\'re already logged in. Update your password here.'), 'flash/success');
        $this->redirect(array('action' => 'profile'));
    }

    if (!empty($token)){

        if ($user = $this->User->findBytoken($token)){
            $id = $user['User']['id'];
            $this->User->id = $id;
        }
        elseif (count($this->User->findBytoken($token) == 0)){
            $this->Session->setFlash(__('Your password reset link was invalid. Please request a new one'), 'flash/error');
            $this->redirect(array('action' => 'forgot_password'));
        }

        if (strtotime($user['User']['token_expires']) < strtotime('now')){
            $this->Session->setFlash(__('For your safety, your reset link has expired after 24 hours. Please request a new one'), 'flash/error');
            $this->redirect(array('action' => 'forgot_password'));
        }   

    } else {

        $this->Session->setFlash(__('You need a reset link to do that. Request one with your email address here.'), 'flash/success');
        $this->redirect(array('action' => 'forgot_password'));
    }

    if (!$this->User->exists($this->User->id)) {
        throw new NotFoundException(__('Invalid user'));
    }

    if ($this->request->is('post') || $this->request->is('put')) {
        $this->request->data['User']['token'] = null;
        $this->request->data['User']['token_expires'] = null;
        $this->request->data['User']['modified'] = false;
        unset($this->request->data['User']['image']);
        if ($this->User->save($this->request->data, $validate = false)) {
            $this->Session->setFlash(__('Your password has been updated. You can log in with your new password now'), 'flash/success');
            $this->redirect(array('action' => 'login'));
        } else {
            $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
            $this->set('user', $this->User->find('first', $options));
            $this->Session->setFlash(__('Your password could not be updated. Please, try again.'), 'flash/error');
        }
    } else {
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->set('user', $this->User->find('first', $options));
        $this->request->data = $this->User->find('first', $options);
    }
}

【问题讨论】:

    标签: php cakephp cakephp-2.0 cakephp-2.4


    【解决方案1】:

    来自 CakePHP 文档http://book.cakephp.org/2.0/en/models/data-validation.html

    “on”键可以设置为以下任一值: “更新”或“创建”。这提供了一种机制,允许某个 在创建新记录期间应用的规则,或 在更新记录期间。

    如果规则定义了“on”=>“create”,则只会强制执行该规则 在创建新记录的过程中。同样,如果它被定义为 ‘on’ => ‘update’,它只会在更新一个 记录。

    public $validate = array(
        'email' => array(
            'rule1' => array(
                'rule'       => array('email'),
                'required'   => true,
                'allowEmpty' => false,
                'message'    => 'Email is invalid'
            ),
            'rule2' => array(
                'rule'       => 'isUnique',
                'required'   => true,
                'allowEmpty' => false,
                'on'         => 'create',
                'message'    => 'Email is not unique'
            )
    );
    

    或者,您可以在控制器中关闭仅对电子邮件字段的验证,然后不将其包含在要保存的数据中。

    $this->User->validator()->remove('email');
    unset($this->request->data['User']['email']);
    

    我也强烈推荐PasswordableBehavior

    【讨论】:

    • 是的,但是我在创建和更新时都需要 isUnique 规则。否则,用户可能会在创建帐户后将其电子邮件地址更新为无效的(即其他人的)。
    • 每个字段可以有多个规则。我更新了我的答案以显示如何
    • $this-&gt;User-&gt;validator()-&gt;remove('email'); unset($this-&gt;request-&gt;data['User']['email']); 做到了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多