开发者

Codeigniter return validation error from helper function

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(开发者_C百科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?


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


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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜