我正在尝试为用户注册设置验证,但我遇到了麻烦。当我在 $validation 数组中只有电子邮件、角色和密码字段时(删除其他字段),它可以工作并保存一个新用户。当我尝试添加其他字段时,它会失败并给出 Flash 消息错误“无法保存用户。请重试。”
我很确定这是 re_password 检查。当我删除它的验证时,它可以工作。但是,当密码不同时,re_password 验证确实会显示错误,所以我不确定在哪里查看
这是我的用户表
id | email | password | location | website | role | created | modified
这是验证要求。为了让它保存一个新用户,我必须删除除电子邮件、密码和角色之外的所有内容。
public $validate = array(
'email' => 'email'
,
'password' => array(
'required' => array(
'rule' => array('minLength', '8'),
'message' => 'A password with a minimum length of 8 characters is required'
)
),
're_password' => array(
'required' => array(
'rule' => array('equalTo', 'password' ),
'message' => 'Both password fields must be filled out'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
),
'location' => array(
'valid' => array(
'rule' => array('notEmpty'),
'message' => 'Please select a location'
)
)
);
这是表格(选项数组在上面,认为没有必要显示)
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('re_password', array('type'=>'password', 'label'=>'Re-Enter Password', 'value'=>'', 'autocomplete'=>'off'));
echo $this->Form->input('location', array('options' => $options, 'label' => 'Select Nearest Location'));
echo $this->Form->input('website',array('label'=>'Enter your website, such as www.example.com. '));
echo $this->Form->input('role', array('type' => 'hidden', 'default' => 'user'));
这是 User 模型中的 re_password 检查函数
function check_user_password($userid) {
$salt = Configure::read('Security.salt');
$this->User->id = $userid;
$hashed_password = $this->User->field('password');
// check password
if($hashed_password == md5($data['User']['re_password'].$salt)) {
return true;
} else {
return false;
}
}
最后,这是 UsersController 中的 add 函数
public function add() {
if ($this->request->is('post')) {
$this->User->create(); //create initiates a form on User/add.ctp
if ($this->User->save($this->request->data)) { //save the form data
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('controller' => 'demos', 'action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
如果您还有什么需要看的,请告诉我