Error Validation Class for CodeIgniter Reactor 2.0

Posted by Danny Herran on May 3, 2011 in Backend | 3 comments

CodeIgniter Error Validation Class CodeIgniter lacks of an error handling class for custom errors. The Form Validation class is very nice for forms, the logs are pretty useful as well, but what happens when we need to handle our own errors?. Currently there is no official solution for this so I decided to take the matter in my own hands.

I present you the Error Validation Class.

What does it do?

It handles custom errors for your routines. You can later run an error test and it will return true or false depending on the evaluated conditions. It also comes with a helper so you can quickly print errors and integrate it to your application very easily.

How does it work?

It is a pair of a Class and Helper. The class will handle the error management, and the helper has some nice functions to echo your custom errors on the application views.

How do I use it?

Download the package, copy both files to your application directory and follow the examples. It works very similar to the Form Validation Class, so it wont be much trouble for you.

Setting Errors

The Error Validation Class lets you set as many errors as you need for your algorithms conditions. To set errors you will use the set_error() method:


The above method takes one parameter as input, which is the error message you want to save. Here is an example of a basic error validation controller:

class Home extends CI_Controller {
	function __construct()
		// Loading the error validation class, it can be autoloaded if you like
		// Session class is already autoloaded

	function index()
		$user_id = $this->session->userdata('u_id');
		$user_credits = $this->session->userdata('u_credits');

			$this->error_validation->set_error('You must be a registered user to participate.');

		// Checking if the user has credits...
		if($user_credits == 0)
			$this->error_validation->set_error('You don\'t have enough credits to participate.');

		if ($this->error_validation->run())
			// Process your data and do your operations
			// Load a view, whatever you need
			// Process your data and do your operations
			// Load a view, whatever you need

Running a Validation

The run() method performs a global check and verifies if an error occurred. It returns true when no errors are found, or false otherwise.


You could also mix the Error Validation Class with the Form Validation Class for more complex routines:

if ($this->form_validation->run() && $this->error_validation->run())
	// Test passed

Showing Errors

The Error Validation Class has 3 functions (helpers) you can use to display the error messages: first_error(), last_error() and all_errors(). All the functions accept 2 parameters: the open tag and close tag. If empty, a paragraph tag will be used as default.

echo all_errors('<span class="error">', '</span>');
echo first_error('<p>', '</p>');
echo last_error('<div>', '</div>');

You can use the above functions inside a view to display the error messages generated in your controller.

Changing the Error Delimiters

By default, the Error Validation class adds a paragraph tag (<p>) around each error message shown. You can either change these delimiters globally or individually.


$this->error_validation->set_error_delimiters('<div class="error">', '</div>');


echo first_error('<div>', '</div>');

I hope you find this useful. This isn’t a complicated class but it has been tested intensively. If you encounter an error caused by the class or want to suggest a feature, do not hesitate to comment below.

Have fun!