0

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

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

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

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

1 Comment

callback_ is method to call the validation or function to validation .the you have to start with callback_ and your function like callback_callback_username_available

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.