Multiple callbacks and regular expressions in callback

#1
[eluser]JamieBarton[/eluser]
Hi guys,

I haven't done PHP let alone Code Igniter for a good 3 months now and diving back into it with a small application I'm making for my friends university folks. They're wanting to be able to register, post comments about their group project and post to-dos etc..

Now you've got the idea of the project, you'll understand my regular expression needs.

I want to be able to get users to register, by adding in their university e-mail address. I need to set some rules so it will only accept emails which have @university.ac.uk. It is important about the .ac.uk as that is the most common e-mail ending for universities in the UK. So nobody else outside can join. We might expand the project more in future so I don't want to narrow it down to just one university @ email etc..

Next thing is checking if that user exists by looking into the database for that email.

I know I want to be running a callback for the email exists, such as;

Code:
function user_exists($user) {
    $query = $this->db->get_where('users', array('username' => $user));
    
    if($query->num_rows() > 0) {
    
        $this->form_validation->set_message('user_exists', 'The email exists, try another one.');
        
        return FALSE;
    
    } $query->free_result(); return TRUE;
    
}

Obviously, it's going to have to check the regular expression first, then lookup the database if the user is registering as theres no point in looking in the database if the expression is wrong and there is no .ac.uk in there. Maybe I just answered my own question of in what order things need to come, but what / where would I put the regular expression, and what would it be, and how do I define the correct messages, "Not a valid university email address".


All the best, thanks for any help in advance.


Jamie

#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…


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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