我有一個頁面,用戶可以在其中更改密碼。該表格包含3個字段:當前密碼(old_pass),新密碼(pass)和新密碼確認(pass_confirm)。問題是,這些字段未經驗證,並且允許使用空白字段,儘管它在模型定義中被禁止。我不知道爲什麼,我在註冊中有一個類似的表單,但是一個工作正常,並顯示這些字段的驗證錯誤。當我顯示$this->User->validationErrors
時,數組是空的。但驗證完成後,我有用戶名驗證,它只在用戶創建時處於活動狀態,當我爲所有User
表單(包括此更改密碼錶單)激活它時,驗證錯誤將在此處正常顯示。cakephp不驗證密碼字段
<?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;
}
順便說一句,當我取消在validate_password
功能return false;
行,什麼都不會發生這樣的方法不根本就是所謂的。
我現在的儲蓄與$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));
}
}
}
你有沒有找出個原因更短,更是問題嗎?我有完全相同的問題。奇怪的是,當我在保存失敗後對控制器中的validationErrors執行調試調用時,change_password操作顯示保存了一些錯誤。但是,仍然沒有出現在表格中。 – tokes
實際上等一下。當你調用'save'時,你只傳入用戶的ID和完整的密碼。你不通過'pass'和'pass_confirm'。難道這就是爲什麼它不能對任何事情進行驗證? – tokes