【发布时间】:2013-08-19 23:43:38
【问题描述】:
我正在创建一个表单,以便用户可以更改他们的密码。此表单在我的设置控制器中,但我将数据保存到我的用户表中。
我有以下表格
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 而不是设置中提取验证
$this->User->set($this->request->data);
if($this->User->Validates() == true){
【问题讨论】:
-
不知道为什么我没有发现...现在我很困惑,你在那里做什么?为什么要使用
User模型来保存属于Setting模型的东西? -
@ndm 它被保存在用户表中,而不是设置表中。我正在从 SettingsController/model 执行此操作
-
好的,但是您希望它如何工作?您正在对
Setting模型定义验证(至少当您说“这是我的设置模型”时我假设是这样),但是您使用的是User模型,这就是不对我来说没有意义,我不确定这是否是我应该在回答中详细说明的错误,或者您是否正在做一些时髦的事情,例如扩展Setting模型或其他东西。 -
@ndm 那么如何将它保存到用户表中呢?我唯一的选择是把它放在 UsersController 中吗?我只能将我正在验证的模型保存到表中吗?
标签: php validation cakephp model