Multiple callbacks and regular expressions in callback

#2
[eluser]sqwk[/eluser]
Split the validation into two functions: (Put them in your controller at the bottom)

Code:
function _check_email_ends_with_ac($str) {
    return preg_match("/^(.*)(\.ac.uk)+$/", $str) ? true : false;
    // Don't trust me on the regex ;-)
}

function _is_in_db($email) {
    // Get CI instance & load db
    $CI =& get_instance();
    $CI->load->database();

    // Query db
    $id = $CI->db->select('id')->from('table')->where('email', $email)->get();

    if ($this->db->affected_rows() == 1)
        return TRUE;
    else
        return FALSE;
}

Prefix the functions with _ to prevent them being called directly.

inside your controler you would do something like this:

Code:
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|callback__check_email_ends_with_ac|callback__is_in_db');
$this->form_validation->set_message('callback__check_email_ends_with_ac', 'Not a university email.');
$this->form_validation->set_message('callback__is_in_db', 'The email is not in the DB.');

if ($this->form_validation->run() == TRUE) {

    // Do stuff

}

Also check out http://ellislab.com/codeigniter/user-gui...ation.html to find out more about setting error messages, etc…


Messages In This Thread
Multiple callbacks and regular expressions in callback - by El Forum - 09-04-2010, 02:35 PM
Multiple callbacks and regular expressions in callback - by El Forum - 09-05-2010, 04:52 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.