I want to create a callback function that is used during validation to check if the username / email address is already in the data开发者_开发百科base... 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
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.
// 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
}
精彩评论