Codeigniter – Code Igniter form_validation load fatal error

codeigniter

I am using code igniter 2.0.3.

When I try loading form_validation in my controller $this->load->library('form_validation'), I get an error message:

Fatal error: Call to a member function set_rules() on a non-object in D:\Installation\xampp\htdocs\MyApp\application\controllers\login.php on line 44

On line 44 in login.php, this is what I have:

$this->form_validation->set_rules('name', 'Name', 'trim|required');

When I load form_validation in autoload.php, there are no errors.

Why is this happening?

Best Answer

The only way I could reproduce that error was to flip-flop the position of library and the validation:

Causes Error -
$this->form_validation->set_error_delimiters('name', 'Name', 'trim|required');
$this->load->library('form_validation');

Works -
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('name', 'Name', 'trim|required');

You're calling a method of the class before the class is loaded. This would also explain why autoloading the validation produced no error for you.

Related Topic