始终在Codeigniter form_validation回调中返回TRUE

时间:2015-11-04 06:43:05

标签: php codeigniter

我的回调函数有问题。我正在创建一个回调函数,可以验证输入的用户名是否与现有用户名相同。如果不相同则验证是否已经存在。如果不存在则验证必须通过。

以下是我在此过程中所做的事情:

  1. 我尝试通常验证字段(必需,max_length,min_length)==确定
  2. 输入已存在的用户名=确定
  3. 输入新用户名= NOT OK
  4. 当我尝试输入新用户名时。它说已经存在了。当我检查数据库时,它已经更新了我的用户名。

    以下是我的代码中的内容:

    CONTROLLER

    $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 **/
    

    MY HELPER CALLBACK

    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;
                }
            }
        }
    

    你能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

如果您的数据库中存在用户,则必须return FALSE而不是return TRUE

将您的查询更改为

 $ci->db->select("user_id");
 $ci->db->where('username',$username);
 $checkValid=$ci->db->get("user");
 $num = $checkValid->num_rows(); //counting row

 if($num>0){
     return FALSE;
 }else{
     return TRUE;
 }

答案 1 :(得分:1)

使用num_rows()获得更好的体验。另请参阅return语句

 $queryValid = $ci->db->query("SELECT * FROM user WHERE username = " . $ci->db->escape($username) . "");
    //$checkValid = $queryValid->row_array();   <-- remove this line

    if($queryValid->num_rows() > 0) {
        return FALSE;
    } else {
        return TRUE;
    }
相关问题