我的回調函數有問題。我創建了一個回調,可以驗證輸入的用戶名是否與現有的用戶名相同。如果不相同,則驗證是否已經存在。如果不存在,則驗證必須通過。總是在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)。「''? (與周圍的單引號?) –