我正在创建一个表单,以便用户可以更改其密码。此表单在我的设置控制器中,但我将数据保存到我的用户表。
我有以下表格
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
) ) )
我不确定这是否是我想要的,但它正在使用设置表,我将保存到users表。我将该值更改为用户(通过手动设置该函数中的值),但它没有更改任何内容。
当我按照建议使用以下内容时,它会从UserModel而非设置
中提取验证$this->User->set($this->request->data);
if($this->User->Validates() == true){
答案 0 :(得分:4)
好的,让我们从一个全新的答案开始吧。这里有各种各样的问题。
您正在Setting
模型上定义验证规则和内容,但在您的代码段中,您使用User
模型来保存数据,因此,如果有的话,它将应用验证User
模型上定义的规则。因此虽然可以使用不同的模型进行验证而不是保存,但我不建议这样做,我会使用User
模型来完成所有这些工作。
这意味着,将验证规则和方法放在User
模型中(希望链接到users
表)。
您对模型名称使用小写/复数表示法,它应该是以大写字母开头的单数:
$this->Form->create('Setting');
$this->request->data['Setting']
请注意,这仅用于演示目的,如上所述,您应使用User
模型,即:
$this->Form->create('User');
$this->request->data['User']
Model::saveField()
仅验证显式传递给它的数据,即它只验证password
字段,并且您无法访问验证方法中的任何其他字段,因为数据不是在模型上设置。
因此,您必须使用Model::save()
并提供允许字段列表(您还可以使用Model::set()
):
$this->User->id = AuthComponent::user('id');
$this->User->save($this->request->data, true, array('password', 'repass'));
或者您trigger validation manually:
$this->User->id = AuthComponent::user('id');
$this->User->set($this->request->data);
if($this->User->validates() === true)
{
$this->User->saveField('password', Security::hash($this->request->data['User']['password'], NULL, true));
}
请注意,在此方案中,您还可以使用其他模型进行验证,即:
$this->Setting->set($this->request->data);
if($this->Setting->validates() === true)
{
$this->User->saveField('password', Security::hash($this->request->data['User']['password'], NULL, true));
}
但是,我认为最好使用User模型,必要时动态更改验证规则以满足您的需求。例如,删除规则:
$this->User->validator()->remove('field_x')
->remove('field_y')
->remove('field_z');
或者通过覆盖验证规则:
$this->User->validate = array
(
...
);
但是,两者最好都是在模型中完成的。
Model::saveField()
最后但并非最不重要的是,Model::saveField()
方法有3个参数,其中第三个参数是boolean
值(默认为false
),定义是否使用验证或采用array
,validate
和callbacks
选项的counterCache
。
因此,您必须将true
设置为第三个参数才能完全由Model::saveField()
触发验证:
$this->User->saveField('password', Security::hash($this->request->data['User']['password'], NULL, true), true);
PS。你知道你目前正在两次哈希吗?首先直接将数据传递给Model::saveField()
,然后再传递给Model::beforeSave()
。