我的回调函数有问题。我创建了一个回调,可以验证输入的用户名是否与现有的用户名相同。如果不相同,则验证是否已经存在。如果不存在,则验证必须通过。总是在Codeigniter form_validation回调中返回TRUE
这是我在这个过程中所做的:
- 我尝试正常验证领域(需要MAX_LENGTH,MIN_LENGTH)==已经存在的用户名OK
- 输入= OK
- 输入一个新的username = NOT OK
当我尝试输入新的用户名。它说它已经存在。当我检查数据库时,它已经更新了我的用户名。
这是我在我的代码:
控制器
$this->form_validation->set_rules('username', 'Username', 'required|trim|min_length[8]|max_length[30]|username_check');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('phone1', 'Primary Contact', 'required');
$this->form_validation->set_rules('address1', 'Primary Address', 'required');
$this->form_validation->set_rules('birthday', 'Birthday', 'required');
$this->form_validation->set_rules('gender', 'Gender', 'required');
if($this->input->post('password1')) {
$this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[8]|max_length[16]');
$this->form_validation->set_rules('password2', 'Confirm Password', 'required|trim|matches[password1]');
}
if($data['role_id'] == 1 || $data['role_id'] == 2) {
$this->form_validation->set_rules('role', 'User Role', 'required');
$this->form_validation->set_rules('status', 'Status', 'required');
}
if($this->form_validation->run() == TRUE) {
$username = strtolower($this->input->post('username'));
$password = $this->input->post('password1');
/** some code here **/
帮助我的回调
if(!function_exists('is_username_exists')) {
function is_username_exists($username) {
$ci =& get_instance();
$ci->db->select('user_id');
$ci->db->where('username', $username);
$checkValid = $ci->db->get('user');
$num = $checkValid->num_rows();
if($num > 0) {
return FALSE;
} else {
return TRUE;
}
}
}
if(!function_exists('username_check')) {
function username_check($username) {
$ci =& get_instance();
$current_value = $ci->flx_user->getUsername();
//check if the input value is same as the saved value
if($current_value != $username) { //if not same check if already existed
$is_available = is_username_exists($username);
if(!$is_available) {
$ci->form_validation->set_message('username_check', 'Username already taken. Please try again.');
return FALSE;
} else {
return TRUE;
}
} else {
// if the same just bypass it
return TRUE;
}
}
}
你能帮助我吗?
岂不是:'其中username =“”。 $ ci-> db-> escape($ username)。“''? (与周围的单引号?) –