CodeIgniter Forums
callback_ won't work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: callback_ won't work (/showthread.php?tid=29641)



callback_ won't work - El Forum - 04-16-2010

[eluser]babazumbula[/eluser]
Hello there,
I'm doing simple checking if username already exists in database. To achieve this in model function I've got
Code:
function existing_user(){
        $this->db->where('username',$this->input->post('username'));
        $query = $this->db->get('membership');
        if($query->num_rows() == 1){ $this->form_validation->set_message('existing_user', 'This %s is taken') ;
                return false;}
        else{return true;}
      }
In the controller function I've got
Code:
function create_member(){
        $this->load->library('form_validation');
        $this->load->model('membership_model');

        $this->form_validation->set_rules('first_name','Ime','trim|required');
        $this->form_validation->set_rules('last_name','Prezime','trim|required');
        $this->form_validation->set_rules('email_address','E-mail','trim|required|valid_email');
        $this->form_validation->set_rules('username','Korisničko ime','trim|required|callback_existing_user');
        $this->form_validation->set_rules('password','Lozinka','trim|required|max_lenght[32]');
        $this->form_validation->set_rules('password2','Ne poklapa se','trim|required|matches[password]');

        if($this->form_validation->run() == FALSE){
            $this->signup();
        }
        else{
            $this->load->model('membership_model');
            
            if($this->membership_model->create_member()){
                $data['main_content'] = 'signup_successful';
                $this->load->view('includes/template',$data);
            }
            else{
                $this->signup();
            }
        }
    }

But every time I try to create account with existing username in database I don't get an error.Every time the duplicate username is inserted into database. I tried even to simplify the model function like
Code:
function existing_user(){
       return false;
      }
which should never insert any data into database, but no use. I'm still getting inserts into database.
Where I went wrong?
Thnx in advance!


callback_ won't work - El Forum - 04-16-2010

[eluser]davidbehler[/eluser]
The form validation looks for the callback method in your controller.

So in your case you would have to do sth like this:
Model
Code:
function existing_user(){
        $this->db->where('username',$this->input->post('username'));
        $query = $this->db->get('membership');
        if($query->num_rows() == 1){
                return true;
       }
        else{return false;}
      }

Controller
Code:
function existing_user(){
    if($this->Model_name->existing_user()) {      
      $this->form_validation->set_message('existing_user', 'This %s is taken') ;
       return false;
    } else {
      return true;
    }
}



callback_ won't work - El Forum - 04-16-2010

[eluser]babazumbula[/eluser]
Yeah,Now it works perfectly! Thnx!