2

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?

2 Answers 2

2

I'm not 100% sure why making the function private ruined your error messages, but I can suggest a workaround:

Form validation rules permit any function that accepts up to 1 parameter to be used as a validation or prep rule. So basically, anything that's available to the current script is available as a form validation rule as well, including your valid_username() function. Try this (just add it to your other rules):

$this->form_validation->set_rules('username', 'Username', 'valid_username');

So, you don't need the controller method as a wrapper, and you don't need to use callback_.
See if that works for you.

Aside: I would suggest skipping the function_exists() call when you define valid_username(), unless you're sure you want to allow the function to be "overwritten".

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

1 Comment

Thanks! I did not want to use a callback but for some reason this particular helper function does not work on form validation rules without callback_. Adding underscore in the set_message(_valid_username) seemed to have been the issue.
0

You can use anything that is callable as a rule. Even if it is a function in a helper. Check it out

The right syntax is:

$this->form_validation->set_rules(
   'username', 'Username', array('required', array('valid_username_callable', 'valid_username')));

In the helper, set error message if necessary like this:

$CI = &get_instance();
$CI->form_validation->set_message('valid_username_callable', 'Error message');

Comments

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.