我有一个带有用户注册的 CakePHP 应用程序。在用户页面上,我希望他们能够更新他们的电子邮件和密码。这是我的User
模型:
<?php
class User extends AppModel {
public $name = 'User';
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
),
'range' => array(
'rule' => array('between', 4, 20),
'message' => 'Between 4 and 20 characters'
),
'characters' => array(
'rule' => array('alphaNumeric'),
'message' => 'Alphanumeric characters only'
),
'unique' => array(
'rule' => array('isUnique'),
'message' => 'This username is taken'
)
),
'email' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'An email is required'
),
'validEmail' => array(
'rule' => array('email'),
'message' => 'Please provide a valid email'
),
'range' => array(
'rule' => array('between', 5, 64),
'message' => 'Between 5 and 64 characters'
),
'unique' => array(
'rule' => array('isUnique'),
'message' => 'This email has already been used'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
),
'range' => array(
'rule' => array('between', 5, 64),
'message' => 'Between 5 and 64 characters'
),
)
);
public function beforeSave() {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
我正在使用表单助手来创建表单:
<p>Modify your account settings</p>
<?php echo $this->Session->flash(); ?>
<?php
echo $this->Form->create('User');
echo $this->Form->input('currentPassword', array('type' => 'password'));
echo $this->Form->input('username', array('disabled' => 'disabled', 'value' => $username));
echo $this->Form->input('email');
echo $this->Form->input('newPassword', array('type' => 'password'));
echo $this->Form->end('Update');
?>
我将如何检查当前密码是否有效,检查新的电子邮件和密码是否通过验证规则,然后从我的控制器中更新用户表中的用户记录?