I decided to continue with the series about CodeIgniter, but now I’ll be writing about more specific topics. In this part, I’ll explain how to use the form_validation library to validate form and ensure the correct data reach the database. Even though it’s an specific topic, I’m going to continue using the same application from previous posts.

Introduction

The idea about the validation came from a comment in the previous post (in the Portuguese post). Validations are not new of course and in fact is necessary and common to ensure the data is correct. I haven’t done it previously because this system was only for advanced users, so even with wrong/empty entries you could remove them from the database without any issues.

But, in order to ensure the integrity of the database, I did the update according to the comment. It’s a simple update, so I’ll explain it just for the users maintenance. I’ll explain as well how the validation library works.

Users Maintenance

In the users maintenance there is a controller called user.php, where we have the index(), add(), edit(), remove() and save() methods. In order to validate entries we need to update only where we save the information in the database. The validation happens before saving the data, of course, and we can use the validation library not only to validate, bu also to prepare the data. It’s possible to use PHP’s function strip_tags to remove HTML tags, for example.

So, the only method we need to update is save(). Before, the method was like this:

In order to use the validation library we have to load it. To do that, simply add $this->load->library(‘form_validation’). This library allows us to define rules for each field. After defining the rules, we run the validation with $this->form_validation->run(). This method will return FALSE if it finds any errors.

First, I updated the cancel button for a submit, this way we can check here if the button was clicked and redirect to the ‘user’ controller. After that, we load the validation library and add the rules. The method set_rules accepts three parameters: the field name (from the input), the second is the text the users sees (the label) and, lastly, the rules.

In this case we are creating rules for the email field, using trim, required and valid_email: “trim” will remove blank spaces before and after, “required” means it’s mandatory and “valid_email” checks if it is a valid email. The second rule will ensure the field is not empty. We also create rules for password and level.

We run the validation inside an “if” condition. If it returns FALSE we’ll define the error property and, in case the user_id has been sent, we’ll run the edit() method. Otherwise we will call the add() method. After loading one of these methods, we need a return to stop the save() method execution. As we don’t have the error property in our controller, we need to add it. Do it right after the LEVEL definition.

private $error = false;

This property will be used in the methods edit() and add() to check if the form was valid or not. Le’t add:

if($this->error)
$data['error'] = $this->error;

Paste this piece of code inside the methods edit() and add() just before $this->template->show(‘users_add’, $data);. This way we can update our view to display the errors inside the form in case the variable was defined. Open the file application/views/users_add.php. The updated code will look like this:

Basically that’s what we need to use this library. Something you need to pay attention to:

If you use the set_value function to fill the field value automatically when there are errors in the validation, you need to define a rule for every field, otherwise the function will not find the value. This happens because the function checks if there is an existing validation object ($this->form_validation), if it exists it will return the field values from this object instead of the $_POST variable. Tos olve this problem, even if a field does not need any validation, you can set an empty rule like $this->form_validation->set_rules(‘field’, ‘Field’, ”).

I did similar implementations for the projects and tasks – you’ll find it in GitHub version 1.3.4. Now I’ll explain some other library options.

Creating your own Rules

CodeIgniter allows you to create you own rules using callbacks. All you need to do is create a public method in the same class where you are doing the validation and add the the method name pre-appended with a ‘callback’ as a rule in set_rule(). Example:

Conclusion

In this article I showed how to use the validation library from CodeIgniter. This functionality is one of the most useful ones because it ensures that the input are correct. I created a tag with the code as it is right no, so you can easily download it. It is the tag 1.3.4, taht can be downloaded through the link https://github.com/oscardias/Simple-Task-Board/archive/v1.3.4.zip.