• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loading validation function from library?

#1
[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

#2
[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_vali...to_Models/


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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