views:

151

answers:

2

Hey guys,

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.

A: 

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;
}
bschaeffer
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?
Lukas Oppermann
I think it just checks for `$this->_my_callback()`, so I don't see why this approach wouldn't work in a library.
Kurucu
Well, it does not. I guess it is because it would have to check for $this->CI->_my_callback().
Lukas Oppermann
A: 

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.

Lukas Oppermann
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.
bschaeffer