我正在创建一个表单,以便用户可以更改他们的密码。此表单在我的设置控制器中,但我将数据保存到我的用户表中。
我有以下表格
settings/index.ctp
echo $this->Form->create('settings');
echo $this->Form->input('current_password');
echo $this->Form->input('password');
echo $this->Form->input('repass', array('type'=>'password', 'label'=>'Re-Enter Password'));
echo $this->Form->end(__('Submit'));
这是我的设置模型
function equalToField($array, $field) {
print_r($array); //check to see if it was even being triggered...it's not!
return strcmp($this->data[$this->alias][key($array)], $this->data[$this->alias][$field]) == 0;
}
public function beforeSave() {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
public $validate = array(
'password' => array(
'required' => array(
'rule' => array('minLength', '8'),
'message' => 'A password with a minimum length of 8 characters is required'
)
),
'repass' => array(
'required' => array(
'rule' => array('equalToField', 'password'),
'message' => 'Passwords do not match'
)
)
);
以及我的 SettingsController 中的代码来保存它
$password = Security::hash($this->request->data['settings']['current_password'], NULL, true);
$this->loadmodel('User');
$options = array('conditions' => array('User.' . $this->User->primaryKey => AuthComponent::user('id')));
$user = $this->User->find('first', $options);
if($user['User']['password'] == $password){ //current password match
$this->User->id = AuthComponent::user('id');
$this->User->saveField('password',Security::hash($this->request->data['settings']['password'], NULL, true));
}
else{
$this->Session->setFlash('Current password is incorrect');
}
我做错了什么,验证没有触发?如果可能的话,我宁愿把它保存在我的 SettingsController 中。此外,在任何人提到它之前,我计划将当前密码匹配为验证标准之一......只要我让它工作。
更新 - 我决定做一些挖掘
在 /lib/Model/Model.php 我转到验证器函数并打印了验证器对象,这就是我发现的
([validate] => Array (
[password] => Array (
[required] => Array (
[rule] => Array (
[0] => minLength
[1] => 8 )
[message] => A password with a minimum length of 8 characters is required ) )
[repass] => Array (
[required] => Array (
[rule] => Array (
[0] => equalToField
[1] => password )
[message] => Passwords do not match
) ) )
[useTable] => settings
[id] =>
[data] => Array (
[Setting] => Array (
[settings] => Array (
[current_password] => current_pass
[password] => testpass1
[repass] => testpass2
) ) )
我不确定这是否是我想要的,但它为此使用了设置表,我正在保存到用户表中。我将该值更改为用户(通过手动设置该函数中的值),但它没有改变任何东西。
当我按照建议使用以下内容时,它会从 UserModel 而不是 Settings 中提取验证
$this->User->set($this->request->data);
if($this->User->Validates() == true){