Welcome Guest, Not a member yet? Register   Sign In
Form Validation Callbacks Load From External Source
#3

[eluser]skunkbad[/eluser]
OK, so I figured out how to make for external form validation callbacks. I'm not sure this is the best way, but it's what works. The key for my success was to use a model, in which I can store all the different callbacks as methods. I know to a MVC purist, I am probably committing the ultimate sin using a model for this purpose, but like I said before, it is what works. Should I have made a library? Well, maybe so. Anyways...

The form validation rule is set as normal, then inside the controller I put a method/function like this:

Code:
public function _usernameChecks($uname){
    list($result , $error_string) = $this->data_checks->username($uname);
    if($result === FALSE){
        $this->form_validation->set_message('_usernameChecks', $error_string);
        return FALSE;
    }else{
        return $result;
    }
}

And the model, which currently only has one method (because it took me so long to figure out what to do to make this work) looks like this:

Code:
<?php
class Data_checks extends Model {

    function username($uname){
        if(strlen($uname) < MIN_CHARS_4_USERNAME){
            $response[] = FALSE;
            $response[] = 'Supplied <span class="redfield">%s</span> was too short.';
            return $response;
        }else{
            //Check against user table
            $user_table = $this->authentication->get_table('user_table');
            $this->db->where('user_name', $uname);
            $query = $this->db->get_where($user_table);
            if ($query->num_rows() > 0){ //if user name already exists
                $response[] = FALSE;
                $response[] = 'Supplied <span class="redfield">%s</span> already exists.';
                $query->free_result();
                return $response;
            }else{
                $response[] = $uname;
                $response[] = '';
                $query->free_result();
                return $response;
            }
        }
    }

}

If anyone has any other ideas, let me know.


Messages In This Thread
Form Validation Callbacks Load From External Source - by El Forum - 08-03-2009, 09:20 PM
Form Validation Callbacks Load From External Source - by El Forum - 08-04-2009, 01:18 AM
Form Validation Callbacks Load From External Source - by El Forum - 08-04-2009, 01:22 AM
Form Validation Callbacks Load From External Source - by El Forum - 04-06-2011, 06:28 PM
Form Validation Callbacks Load From External Source - by El Forum - 04-07-2011, 02:04 AM
Form Validation Callbacks Load From External Source - by El Forum - 04-07-2011, 04:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB