Jerielle
Jerielle

Reputation: 7520

Always return TRUE in Codeigniter form_validation callback

I have a problem in my callback function. I am creating a callback that can validate if the inputted username is the same as the existed username. If not the same then validate if already existed. If not existed then the validation must pass.

Here's what I did in the process:

  1. I try to normally validated the field (required, max_length, min_length) == OK
  2. Input already existed username = OK
  3. Input a new username = NOT OK

When I try to input a new username. It said it is already existed. And when I checked the database it already updated my username.

Here's what I have in my code:

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

Can you help me with this?

Upvotes: 1

Views: 767

Answers (2)

Saty
Saty

Reputation: 22532

If user exist in your database you have to return FALSE not return TRUE

And change your query to

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

Upvotes: 2

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

Use num_rows() for better experience. Also see return statements

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

Upvotes: 1

Related Questions