doulai1910 2014-01-18 22:01
浏览 26
已采纳

CakePHP:编辑用户而不更改密码

How do I save users in my CakePHP app without requiring them to change their password each time?

I have code in place to check the two password fields and apply some verification rules, which works great for registration and for changing passwords in the 'edit' view. However, how do I skip the verification rules and saving the password if the password fields are left empty in the Edit view? Obviously, I don't want to skip this requirement on registration.

register.ctp and edit.ctp:

echo $form->create('User');
echo $form->input('username');
echo $form->input('pwd');
echo $form->input('pwd_repeat');
echo $form->end('Submit');

User.ctp validation rules:

'pwd' => array(
        'length' => array(
            'rule'      => array('between', 8, 40),
            'message'   => 'Your password must be between 8 and 40 characters.',
        ),
    ),
    'pwd_repeat' => array(
        'length' => array(
            'rule'      => array('between', 8, 40),
            'message'   => 'Your password must be between 8 and 40 characters.',
        ),
        'compare'    => array(
            'rule'      => array('validate_passwords'),
            'message' => 'The passwords you entered do not match.',
        ),
    ),

and the User.ctp logic before saving:

public function validate_passwords() { //password match check
return $this->data[$this->alias]['pwd'] === $this->data[$this->alias]['pwd_repeat'];
}

public function beforeSave($options = array()) { //set alias to real thing and hash password

    $this->data['User']['password'] = $this->data[$this->alias]['pwd'];
    $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    return true;
}
  • 写回答

4条回答 默认 最新

  • doushan5222 2014-01-18 22:05
    关注
    var $validate = array(
        'pwd' => array(
            'length' => array(
                'rule'      => array('between', 8, 40),
                'message'   => 'Your password must be between 8 and 40 characters.',
                'on'        => 'create',  // we only need this validation on create
            ),
        ),
    
        // if we have a password entered, we need it to match pwd_repeat (both create and update)
        // we no longer need the length validation
        'pwd_repeat' => array(
            'compare' => array(
                'rule'    => array('validate_passwords'),
                'message' => 'Please confirm the password',
            ),
        ),
    );
    
    
    public function validate_passwords() { //password match check
        return $this->data[$this->alias]['pwd'] === $this->data[$this->alias]['pwd_repeat'];
    }
    
    public function beforeSave($options = Array()) {
        // if we have a password, we hash it before saving
        if (isset($this->data[$this->alias]['pwd'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['pwd_repeat']);
        }
        return true;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?