Welcome Guest, Not a member yet? Register   Sign In
callback not working
#1

[eluser]matches[/eluser]
Hello,

I am trying to get callback_ working but it doesn't seem to do anything. When I run this script it just goes straight to the insert statement. Currently I just want to echo my query when I call callback_ to make sure everything is working.

Thanks for any help.

Code:
function usernamecheck($str)
    {
        $this->db->select('userName');
        $this->db->where('userName', $str);
        $query = $this->db->get('users');
        echo $query;
        
        /*if ($str == $query)
        {
            $this->validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }*/
    }
    
        
    function loadData()
    {        
        $userName = $this->input->post('userName');
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $passwordConfirm = $this->input->post('passwordConfirm');
        
        $rules['userName'] = "trim|required|callback_usernamecheck";
        $rules['password'] = "trim|required|matches[passconf]|md5";
        $rules['passconf'] = "trim|required";
        $rules['email'] = "trim|required|valid_email";
        
        $this->validation->set_rules($rules);
        
        $fields['username'] = 'Username';
        $fields['password'] = 'Password';
        $fields['passconf'] = 'Password Confirmation';
        $fields['email'] = 'Email Address';
        
        $this->validation->set_fields($fields);
        
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('register/index');
        }
        else
        {
            $data = array(
                        'userName' => $this->input->post('userName'),
                        'password' => $this->input->post('password'),
                        'firstname' => $this->input->post('firstName'),
                        'lastName' => $this->input->post('lastName'),
                        'address1' => $this->input->post('address1'),
                        'address2' => $this->input->post('address2'),
                        'city' => $this->input->post('city'),
                        'state' => $this->input->post('state'),
                        'zip' => $this->input->post('zip'),
                        'email' => $this->input->post('email'),
                        'signUpDate' => now()
                        );
            $this->db->insert('users', $data);
            redirect("register/registered");
        }            
    }
#2

[eluser]matches[/eluser]
OK, well it turns out callback is working fine I am just need help understanding how to compare the string ($str) passed to it a query of that same string. I don't think I am doing it right here

Code:
function usernamecheck($str)
    {
        $this->db->select('userName');
        $this->db->where('userName', $str);
        $query = $this->db->get('users');
            
        if ($query)
        {
            $this->validation->set_message('usernamecheck', 'Someone is already using the username ');
            return FALSE;            
        }
        else
        {
            return TRUE;
        }
    }
#3

[eluser]mironcho[/eluser]
According to your query, there is no need of additional comparison. Just check if there are some returned rows in the result:
Code:
if ($query->num_rows() > 0)
{
   // username exists, put your FALSE code here
}




Theme © iAndrew 2016 - Forum software by © MyBB