0
votes

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?

2

2 Answers

2
votes

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.

0
votes

Inside the Loader code, CodeIgniter gets the Controller instance to add the loaded objects to via the "get_instance" method.

In your sub-class of Controller, try also running that method and see what gets returned.

In my case I found that it was an autoloaded Controller sub-class that I was not even using or extending in any of my code - it was just a stub class for an idea I had to have all of my page controllers extend a base site controller, which in turns extended CI_Controller. I stress that I was not extending this object but rather bypassing it for the CI_Controller in my own page controller. I have no idea how CodeIgniter got the idea that I wanted to add my instances to that object, but anyway the fix was to remove that object from the autoload libraries list.

Crazy, but maybe it will work for you too.