Error Validation Class for CodeIgniter Reactor 2.0

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:

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:

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!

Abed

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.