Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6206929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:32:16+00:00 2026-05-24T05:32:16+00:00

I have a helper function that checks if a username is valid. In helper

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:32:17+00:00Added an answer on May 24, 2026 at 5:32 am

    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”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a helper class pulling a string from an XML file. That string
I have a rails helper in my application_helper.rb file that looks like this: def
I have a helper function, which basically calls CompareTo on two objects, but does
The problem seems very strange. I have a AJAX helper function within a same
I have a helper assembly which includes a function to identify object types: namespace
I have a helper class that is just a bunch of static methods and
I have a Browser Helper Object project in c# that (obviously) references shdocvw.dll. I
I have a small helper app that I use to inject scripts into html
I have a view helper that manages generating thumbnails for images. The images are
I have a login function in my controller that was getting called before, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.