views:

33

answers:

2

I want to create a callback function that is used during validation to check if the username / email address is already in the database... problem is I just cant seem to get it working

So this is the callback function:

function callback_username_available($username)
{
    if($this->user_model->username_available($username))
    {
        return TRUE;
    }
    else
    {
        $this->form_validation->set_message('username_available', 'ERROR');
        return FALSE;
    }
}

And this is the validation logic :

// setup form validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'username', 'callback_username_available');

if($this->form_validation->run() == FALSE)
{
    // validation errors
}
else
{
    // no validation errors
}

I have been at this for hours and have no idea what i'm doing wrong... both functions are in the same controller and all other standard validation rules work just fine.

Even when i set the callback function to just return FALSE, it still validates the username.

Any ideas guys... its driving me up the wall at the moment :S

A: 

Hope I understand correctly but if you want to check if a username is already in your database you can do something like that: 1.Put this in one of your models:

 function check_name() {

            $submited_username = $this->input->post('username');

            $usernames = $this->db->get_where('your_table', array('username' => $username));

            foreach ($usernames->result() as $username) {

                if($submited_username == $username){
                     return TRUE;
                }
            }

        return FALSE;
    }

2.Use something like that in your view:

form_open('controller_name/your_function');
form_input('username','username');
form_close();

3.something like that in your controller:

function your_function(){

    $this->load->model('Your_model');

        if($this->input->post('username'){

            $this->Your_model->check_name();  //this variable here will have either  false or a true value....
        }

}

There might be some syntax problems because I wrote it in a hurry but you can try and focus on the logic.... hope that helps

rabidmachine9
+2  A: 

to invoke a callback in CI you don't need to name the function " callback_ my_function" - this it would appear is automatically appended.

this should work:

function username_available($username)
{
    if($this->user_model->username_available($username))
    {
        return TRUE;
    }
    else
    {
        $this->form_validation->set_message('username_available', 'ERROR');
        return FALSE;
    }
}

// set the rule
    $this->form_validation->set_rules('username', 'Username', 'callback_username_available');

// lets do this ~

if ($this->form_validation->run() == FALSE)
{
    $this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}

to clarify by calling your function "callback_username_available", CI is attempting to find

callback_callback_username_available() which of course doesn't exist.

Ross
Excellent... you're a genius mate. Thanks for the help :D
Quigley