我有一个页面,用户可以在其中更改密码。该表单包含3个字段:当前密码(old_pass),新密码(pass)和新密码确认(pass_confirm)。问题是,虽然在模型定义中禁止使用,但是字段未经过验证且允许使用空白字段。我不知道为什么,我在注册中有一个类似的形式,但一个工作正常并显示这些字段的验证错误。
当我显示$this->User->validationErrors
时,数组为空。但验证完成后,我的用户名验证仅在用户创建时有效,当我为所有User
表单(包括此更改密码表单)激活它时,验证错误会在此处正确显示。
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php __('Change password'); ?></legend>
<?php
echo $this->Form->input('User.old_pass', array('type' => 'password',
'label' => __('Current password', true)));
echo $this->Form->input('User.pass', array( 'type' => 'password',
'label' => __('New password', true)));
echo $this->Form->input('User.pass_confirm', array('type' => 'password',
'label' => __('Repeat password', true)));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
与这些输入有关的validate
数组部分如下所示:
'pass' => array(
'required' => array(
'rule' => array('custom','/^.*[0-9].*$/i'),
'message'=>'Password must contain numbers',
'allowEmpty' => false
),
'length' => array(
'rule' => array('minLength',8),
'message' => 'Password must be at least 8 characters long')
),
'pass_confirm' => array(
/*'required' => array(
'rule' => 'notempty',
'message' => 'You have to confirm the password',
'allowEmpty' => false
),*/
'validate' => array(
'rule' => 'validate_password',
'message'=>'Your passwords don\'t match!' )
),
'old_pass' => array(
'length' => array(
'rule' => array('minLength',8),
'message' => 'Password must be at least 8 characters long'),
'allowEmpty' => false
)
function validate_password(){
//return false;
$passed=false;
if(isset($this->data['User']['pass_confirm']) && $this->data['User']['pass_confirm']!=''){
if ($this->data['User']['pass'] != $this->data['User']['pass_confirm']){
$this->invalidate('pass');
} else {
$passed=true;
}
}
return $passed;
}
顺便说一句,当我取消注释return false;
函数中的validate_password
行时,没有任何反应,因此根本不会调用该方法。
我正在使用$this->User->save($data)
保存用户,$data
变量包含我想以propper格式保存的数据并且工作正常。唯一的问题是字段未经过验证
编辑:
function change_password() {
$usr = $this->User->find('first', array(
'conditions'=> array(
'User.id' => $this->Auth->user('id')),
'recursive' => 0
));
if ($this->data) {
if ($this->Auth->password($this->data['User']['old_pass'])== $usr['User']['password']) {
$data = array(
'id' => $usr['User']['id'],
'password' => $this->Auth->password($this->data['User']['pass'])
);
if ($this->User->save($data)) {
$this->Session->setFlash(__('Password has been changed.', true));
} else {
debug($this->User->validationErrors);
$this->Session->setFlash(__('Password could not be saved, please try again later.', true));
}
} else {
$this->Session->setFlash(__('The password you entered as your current is not your current password.', true));
}
}
}
答案 0 :(得分:0)
尝试向validate_password方法添加参数:
function validate_password($check){
答案 1 :(得分:0)
你使用字段param吗?您需要向我们展示控制器处理
我的猜测:
$this->save($this->data, true, FIELDS);
FIELDS仅限于某些值(将省略您的密码输入)
顺便说一下:if ( !empty($this->data['User']['pass_confirm']) ) { }
更短,绰绰有余