我正在写一个页面,我的用户可以在其中更改他们的帐户电子邮件和密码。这是控制器动作和视图:
# UsersController.php
public function edit() {
if($this->request->is('post')) {
if($this->User->save($this->request->data)) {
$this->Session->setFlash('Your account has been updated.');
$this->redirect(array('action' => 'edit'));
}
$this->Session->setFlash('There was a problem saving your account settings. Please try again.');
}
// Auto populate form fields
if(!$this->request->data) {
$this->request->data = $this->User->find('first', array(
'conditions' => array('User.id' => $this->Auth->user('id'))
));
}
}
# edit.ctp
<?php echo $this->Form->create('User'); ?>
<?php echo $this->Form->input('currentPassword', array('between' => 'You must enter your password in order to make changes', 'type' => 'password', 'value' => '', 'autocomplete' => 'off')); ?>
<?php echo $this->Form->input('email'); ?>
<?php echo $this->Form->input('password', array('type' => 'password', 'between' => 'Must be atleast 6 characters', 'value' => '', 'autocomplete' => 'off')); ?>
<?php echo $this->Form->input('confirmPassword', array('type' => 'password', 'value' => '', 'autocomplete' => 'off')); ?>
<?php echo $this->Form->end('Save changes'); ?>
现在,我想让用户输入他们当前的密码以进行更改。为了让它工作,我需要运行验证检查以确保他们输入的密码currentPassword
与我在数据库中的密码相匹配。User
我的模型中的验证规则之一是:
'currentPassword' => array(
'custom' => array(
'rule' => 'validateCurrentPassword',
'message' => 'Incorrect password. Make sure you\'re using your current password.'
)
),
以及被调用的相关函数:
public function validateCurrentPassword($data) {
debug($data);
return false;
}
到目前为止一切都很好,但是有一些非常奇怪的行为。Cake 似乎只在加载两个页面后才验证此字段。例如,如果我输入错误的值并按“保存更改”,页面会刷新,但不会弹出验证错误。如果我输入另一个错误的值,我会收到验证错误。出于某种原因,我需要提交两次表单以进行验证。
谁能弄清楚这是为什么?