• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
validation problem

#1
[eluser]bigtimslim[/eluser]
This is my first time using the validation class and as far as I can tell everything is working how it should. However, when I register and the username passes the checkUsername callback I get this error: "Trying to get property of non-object" on the line that read "if (strtolower($str) == strtolower($row->username))"

If the username is taken, it detects and gives the validation message. If the username isn't taken the data is inserted into the db fine.

Can anyone tell me what is causing that message?


from home model:
Code:
// User Registration //    
    function register()
    {    
        $this->load->library('validation');

        $data['title'] = "New User Registration";
        $data['heading'] = "blah";
        $data['tab'] = 'blah';        
        $this->load->view('home/register_view.php', $data);                
    }    
    
    function checkUsername($str)
    {
        $this->load->model('user_model');
        $query = $this->user_model->getUser($str);        
        $row = $query->row();

        if (strtolower($str) == strtolower($row->username))
        {
            $this->validation->set_message('checkUsername', 'The %s "'. $str . '" is already taken.');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }        
    
    function addUser()
    {    
        $this->load->library('validation');
            
        $rules['username']    = "trim|required|min_length[5]|max_length[24]|xss_clean|callback_checkUsername";
        $rules['email']        = "trim|required|min_length[8|max_length[48]|valid_email";        
        $rules['password']    = "trim|required|min_length[5|matches[pass2]";
        $rules['pass2']    = "required";
        $rules['test']    = "trim";
        
        $this->validation->set_rules($rules);
        $this->validation->set_error_delimiters('<div class="error">', '</div><br />');
            
        if ($this->validation->run() == FALSE)
        {        
            $this->load->view('home/register_view.php');                                    
        }
        else
        {
            $formData['username'] = $this->input->post('username');
            $formData['email'] = $this->input->post('email');
            $formData['password'] = $this->input->post('password');
            $formData['test'] = $this->input->post('test');
            $formData['regdate'] = date("Y/m/d");            
            $this->load->model('user_model');
            $this->user_model->insertUser($formData);                
            $data['msg'] = 'Your account has been created!';        
            $this->load->view('home/home_view.php', $data);                        
        }        
                
    }

from user_model
Code:
function getUser($user)
    {
        return $query = $this->db->get_where('user', array('username' => $user));
    }

    function insertUser($data)
    {
        $this->db->insert('user', $data);
    }

#2
[eluser]mdowns[/eluser]
Try checking the row only if num_rows > 0:

Code:
$query = $this->user_model->getUser($str);
if($query->num_rows()>0){
  $row = $query->row();

  if(strtolower($str) == strtolower($row->username)){
    $this->validation->set_message('checkUsername', 'The %s "'. $str . '" is already taken.');
    return FALSE;
  }
}

return TRUE;

#3
[eluser]bigtimslim[/eluser]
Ahhhh thank you.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.