CodeIgniter Forums
Loading validation function from library? - 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: Loading validation function from library? (/showthread.php?tid=22337)



Loading validation function from library? - El Forum - 09-05-2009

[eluser]Maximilian Schoening[/eluser]
Hi,
I have the following function inside a controller:

Code:
/** ----------------------------------------------------------------------
* User login
**/
function login() {
    // if logged in
    if ($this->session->userdata('logged_in')) {
        redirect('/user', 'refresh');
    }
    // if logged out
    else {
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $validation_rules = array(
            array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required|validemail'
            ),
            array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'requied|callback_authenticate'
            )
        );
        
        // validate password
        function authenticate() {
            $this->load->library('authentication');
            
            if ( ! $this->authentication->login($this->input->post('email'), $this->input->post('password'))) {
                return FALSE;
            }
            else {
                return TRUE;
            }
        }
        
        $this->form_validation->set_rules($validation_rules);
        
        if ($this->form_validation->run() == false) {
            $this->load->view('user/login');
        }
        else {
            redirect('/user', 'refresh');
        }
    }
}
// -----------------------------------------------------------------------

As you can see I am validating the password field with a custom validation function (authenticate()) but all this function does is call another function inside a library. Can I load the library function login() directly into the validation rules?

Thanks,
Max


Loading validation function from library? - El Forum - 09-05-2009

[eluser]pistolPete[/eluser]
[quote author="Maximilian Schoening" date="1252201535"]Can I load the library function login() directly into the validation rules[/quote]

Try this extension:

http://codeigniter.com/wiki/MY_Form_validation_-_Callbacks_into_Models/