CodeIgniter Forums
Can you validate a condition without associating a field name and callback? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Can you validate a condition without associating a field name and callback? (/showthread.php?tid=16575)



Can you validate a condition without associating a field name and callback? - El Forum - 03-10-2009

[eluser]hal10001[/eluser]
I want to run a check through a query, but it is associated with multiple fields, not just one (it is a username/password/active user) login check. Can I run this "general" check without using the typical callback approach, and then set a message that says invalid username and password combination? I tried, but it just doesn't return the message.


Can you validate a condition without associating a field name and callback? - El Forum - 03-10-2009

[eluser]hal10001[/eluser]
Well, the documentation is a little short in this area, but it turns out that your rule name doesn't really need to match your field name. You can do something like this:

Code:
$this->form_validation->set_rules('is_valid_user', '', 'callback__is_valid_user');

The trick is that because is_valid_user is not a form field, you are passing no string value into your callback function. However, whatever post values were sent through are still available in your callback, so you can just use those to do whatever you need.

Code:
function _is_valid_user()
{
    $user_login_id = $this->input->post('user_login_id');
    $user_password = $this->input->post('user_password');
    
    // do something and return true or false;
}



Can you validate a condition without associating a field name and callback? - El Forum - 03-11-2009

[eluser]TheFuzzy0ne[/eluser]
You don't have to use a validation callback. My auth library has a method called doLogin(). It returns TRUE or FALSE. If it returns FALSE, I can use $this->auth->error_string to get the error string and display it.