[英]Checking for 'isUniqueUsername' in cakephp doesn't seem to work?
I have a register form where when a user registers it asks for their Username, Password and email. 我有一个注册表,当用户注册时,它会询问他们的用户名,密码和电子邮件。 For the username, I have a few rules such as:
对于用户名,我有一些规则,例如:
With that being said, I can't seem to get my form to catch duplicate usernames, I was hoping someone could help me solve this! 话虽这么说,我似乎无法让我的表格捕捉重复的用户名,我希望有人可以帮助我解决这个问题!
Here is my User.php Model's Validate: 这是我的User.php模型的Validate:
public $validate = array(
'username' => array(
'nonEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required',
'allowEmpty' => false
),
'between' => array(
'rule' => array('between', 5, 15),
'required' => true,
'message' => 'Usernames must be between 5 to 15 characters'
),
'unique' => array(
'rule' => array('isUniqueUsername'),
'message' => 'This username is already in use'
)
)
);
I removed the 3rd rule (alpha/number/dash) for the sake of the example 为了示例,我删除了第三条规则(alpha /数字/破折号)
Followed by my function to check my DB for the username: 接下来是我的功能,用于检查数据库中的用户名:
function isUniqueUsername($check) {
$username = $this->find(
'all',
array(
'fields' => array(
'User.id',
'User.username'
),
'conditions' => array(
'User.username' => $check['username']
)
)
);
if(!empty($username)){
if($this->data[$this->alias]['id'] == $username['']['id']){
return true;
}else{
return false;
}
}else{
return true;
}
} }
here is the Controller function for "Register" 这是“注册”的控制器功能
public function register() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been created'));
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('The user could not be created. Please, try again.'));
}
}
}
Change 更改
'unique' => array(
'rule' => array('isUniqueUsername'),
'message' => 'This username is already in use'
)
To 至
'unique' => array(
'rule' => 'isUnique',
'message' => 'This username is already in use'
)
You do not need that function to check for uniqueness, Cake does it for you with magic. 您不需要该功能来检查唯一性,Cake用魔术为您做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.