I created a helper for checking if a user id exists in my user database table:
if ( ! function_exists('valid_user'))
{
function valid_user($user_id)
{
$ci=& get_instance();
$ci->load->database();
$ci->db->select('id');
$ci->db->where('id', $user_id);
$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
{
return FALSE;
}
}
}
I added the function to my validation rule like so
$this->form_validation->set_rules('user_id', 'User ID', 'required|xss_clean|max_length[11]|is_natural_no_zero|valid_user');
It does not perform the valid_user function. What am I doing wrong here?
In my previous experience, I usually added a validation function (in your case,
valid_user) in the same place where the callback is called.For example, I would put
valid_usermethod in a users_controller where one of the registration methods will invoke thevalid_usermethod.Also, it seems that, in your
set_rules, you have to setcallback_valid_usernotvalid_useraccording to the Codeigniter user guides.http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks