Error Validation Class for CodeIgniter Reactor 2.0

Posted by on May 3, 2011 in CodeIgniter, PHP | 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:

$this->error_validation->set_error();

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()
	{
		parent::__construct();
		// Loading the error validation class, it can be autoloaded if you like
		$this->load->library('Error_validation');
		// Session class is already autoloaded
	}

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

		if(!$user_id)
			$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
		}
		else
		{
			// 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.

$this->error_validation->run()

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.

Globally:

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

Individually:

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!

3 Comments

  1. Abed
    May 5, 2011

    Impeccable timing. I just started using CodeIgniter less than 2 weeks ago and was about to start working on something exactly like this for my needs (an error handling functionality with user output, and something numbered as well). Also I was following some peoples’ customization of the form validation class, just added an additional method to work in tandem with that extension (though I’m not sure if that’s bad practice?) in the case I want to force both a form validation error as well as save an error when the source can be human or otherwise.

    Thanks for sharing. I’ve just started using it and it really is exactly what I needed.

    Reply
    • Danny Herran
      May 5, 2011

      No problem Abed. Hope you enjoy it!

  2. Jona
    January 16, 2012

    Looks very CodeIgniter :) Will give it a try this week. Thanks!

    Reply

Leave a Reply