• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
callback_ won't work

#1
[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!

#2
[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;
    }
}

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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