0
votes

I am trying to create a simple login process using CodeIgniter 3. However, I am getting this error every time I click submit on the login form:

A PHP Error was encountered

Severity: Error

Message: Call to undefined method CI_Form_validation::setrules()

Filename: controllers/Login.php

Line Number: 33

Backtrace:

This is my controller code:

class Login extends CI_Controller
{
    public function __construct()
{
    parent::__construct();
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->library('session');
    $this->load->model('login_model');

}

public function index()
{
    if(isset($this->session->userdata['logged_in']))
    {
        $this->load->view('template');
    }else{
        $this->load->view('login');
    }
}

public function auth_user()
{
    $this->form_validation->setrules('username', 'Username', 'trim|required|xss_clean');
    $this->form_validation->setrules('pass', 'Password', 'trim|required|xss_clean');

    if($this->form_validation->run() == TRUE)
    {
        echo 'IT WORKS';
        $data = array(
          'username' => $this->input->post('username'),
          'password' => $this->input->post('pass')
        );
        //check user credentials
        $result = $this->login_model->login($data);

        if($result == TRUE)
        {   //get user info
            $session_data = array(
                'username' => $result[0]->username
            );
            $user_id = $result->staffID;
            $this->login_model->staff_logs($user_id);
            $this->session->set_userdata('logged',$session_data);
            $this->load->view('template');
        }
    }else{
        echo 'IT not work';
    }
}
}

I am using PHPStorm, when I hover form_validation, it says that Field 'form_validation' not found in class Login.

Your response is highly appreciated. Thanks!

1

1 Answers

0
votes

you are missing the _ from set_rules(....)

change your code to

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('pass', 'Password', 'trim|required|xss_clean');

from

    $this->form_validation->setrules('username', 'Username', 'trim|required|xss_clean');
    $this->form_validation->setrules('pass', 'Password', 'trim|required|xss_clean');