4

my problem is the following: I am writing a login library. This library has a function _validation() and this uses the validation library to validate the data. With using normal validation methods it works just fine, but using a callback function just does not work. It is not called.

I call it like this.

$this->CI->form_validation->set_rules('user', 'Username', 'required|callback__check_user');

The functions name is _check_user and it uses the username _check_user($user). The function itself works fine and I can also call it in the class ($this->_check_user('username')) with a working result.

I am guessing, there might be a problem because I am not workin in a controller so I have a CI instance $this->CI instead of just the original instance $this->

Does anyone have a clue how to fix this?

Thanks in advance.

2
  • Does 'required' fire? Did you set an error message for the callback? If you echo something out in the call back, do you see it? is your call back function protected or private or anything? Commented Jun 12, 2010 at 20:23
  • Well, require does fire. I did set an error message (require evokes it). And yes, the callback is private but not protected. Commented Jun 13, 2010 at 2:04

2 Answers 2

4

Hey, I figured out a way that works for me. By just extending the Form_validation library in MY_Form_validation.php you can create custom validation methods. I think it is a clean way and it works perfectly fine. I build the below validation method to check for existing usernames and passwords. $value is something like table_name.fieldname. I have not message set so that it will use the _exist messages from the lang files.

/**
 * Exist
 *
 * checks if the entry exists in the database
 * returns a boolean
 *
 * @access  private
 * @param   string
 * @param   field
 * @return  boolean
 */
function _exist($str, $value)
{       
    list($table, $column) = explode('.', $value, 2);    
    $query = $this->CI->db->query("SELECT COUNT(*) AS count FROM $table WHERE $column = '$str'");
    $row = $query->row();
    return ($row->count > 0) ? TRUE : FALSE;
}

Thanks for your help though.

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

2 Comments

I think that's probably the best way to do it. It's available everywhere you use the form validation library, not just inside single custom library you made.
Just for everyone's FYI, one thing that tripped me up as I tried to extend the form validation library using MY_Form_validation, is to get rid of the callback_ in the ->form_validation->set_rules(...) since it is no longer a callback within a controller. Please notice this in @JordanArseno's answer above where belongstowork is NOT prefaced with callback_
0

The form validation callback will only fire on a method inside the current controller.

Just do this in the controller you're using the callback:

function _check_user($user)
{
    $this->load->model('login');
    $result = $this->login->_check_user($user);
    return $result;
}

3 Comments

Well, that does not work, because I am building a library and not a controller. Is there no other way to have a working callback?
I think it just checks for $this->_my_callback(), so I don't see why this approach wouldn't work in a library.
Well, it does not. I guess it is because it would have to check for $this->CI->_my_callback().

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.