I have a helper function that checks if a username is valid.
In helper file:
if ( ! function_exists('valid_username'))
{
function valid_username($username)
{
$ci=& get_instance();
$ci->load->database();
if (empty($username) OR !preg_match("/^[a-z]([0-9a-z_-])+[0-9a-z]$/i", $username)) //Allowed a-z, 0-9, and underscores. Must end in a-z or 0-9.
{
$ci->form_validation->set_message('valid_username', 'Characters not valid.');
return FALSE;
}
$ci->db->select('username');
$ci->db->where('username', $username);
$ci->db->where('activated', 1);
$ci->db->where('banned', 0);
$ci->db->limit(1);
$query = $ci->db->get('users');
if ($query->num_rows() > 0) //if user exists
{
return TRUE;
}
else
{
$ci->form_validation->set_message('valid_username', 'The %s does not exist.');
return FALSE;
}
}
}
In my controller I am adding the helper to my form validation:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[2]|max_length[50]|callback_valid_username');
//add callback from helper function
function valid_username($username)
{
return valid_username($username);
}
This works well however I have to make the callback private like so:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[2]|max_length[50]|callback__valid_username');
function _valid_username($username)
{
return valid_username($username);
}
After making the callback private I get the following message upon submitting an invalid username:
Unable to access an error message corresponding to your field name.
What am I doing wrong here?