Performing Strict Validation with the Code Igniter PHP Framework

Welcome to the fifth article in our nine-part series on the Code Igniter framework. In the last article you learned a very simple way to use the framework to check information supplied by users through an HTML form. The type of checking done was very simple, however, and user-supplied data must be checked and validated pretty strictly for many purposes. This article will show you how to improve the previous application’s validation mechanism.


Code reusability is a well-known and powerful concept that provides PHP programmers with the capacity for developing complex web applications without having to “reinvent the wheel.” Naturally, in the area dominated by third-party frameworks this concept is really king, since the main goal of a development framework is to accelerate the creation of PHP programs through a set of classes and functions that can be reused any number of times.

Of course, when it comes to picking a particular PHP framework for rapid application development, there’s plenty of options available, and naturally each of them has its own pros and cons. However, before you start scratching your head over which package to choose, you should take a look at Code Igniter (, a piece a software written in PHP 4 that lets you build full-blown PHP applications by using the Model-View-Controller pattern, and best of all, by means of a friendly programming interface.

And speaking of friendly things, you’ll possibly recall that in the previous installment of the series, I explained how to use Code Igniter’s validation class to perform a basic checking process on a simple web form. Essentially, the entire validation procedure was reduced to creating a controller class capable of verifying if each field of the form in question was left empty or not. That’s all.

In addition, based on the result of this process, the controller would either display a simple confirmation web page, or in the worst case, it would redisplay the sample web form, along with an error message indicating which fields should be populated correctly.

But, as I mentioned before, the checking process performed on this online form was pretty simplistic. In this fifth chapter of the series, I’m going to demonstrate how to use Code Igniter’s validation class to validate the data collected through a typical HTML form much more strictly, something that can be very useful for filtering user-supplied information.

Now, it’s time to jump forward and learn in a few easy steps how to perform strict validation on input data with Code Igniter. Let’s get started!

{mospagebreak title=Performing strict validation on user-provided data with Code Igniter}

As I anticipated in the introduction, the validation class that comes included with Code Igniter permits us to apply severe checking rules for the data entered into a specific online form. However, as with each application developed with this framework, it’s necessary to build a controller class that actually performs this validation process.

Then, if the form fails to pass the checking procedure, the controller will redisplay it, along with a descriptive error message. Otherwise, a confirmation web page will be shown instead. Fairly easy to follow, right?

Now that you have grasped the logic that will be implemented by the pertinent controller class, let me show you how it looks. Here it is:

class Validator extends Controller {

function Validator(){

// load controller parent


// load ‘url’ helper


// load ‘form’ helper


// load ‘validation’ class



function index(){

// set validation rules





// set values for repopulating fields

$fields['firstname']=’First Name’;

$fields['lastname']= ‘Last Name’;

$fields['email']=’Email Address’;


// check if user form has been submitted properly

if ($this->validation->run()==FALSE){

// redisplay user form and repopulate fields



// display success web page






Regardless of its short signature, the above “Validator” controller class does many useful things that need to be explained in more detail. First, the constructor loads the corresponding validation class, and the “url” and “form” helpers that you learned about in previous tutorials. As you can see, understanding how this process is achieved is pretty simple.

But the most interesting things happen when the “index()” method is called. As shown before, it first sets specifically the validation rules that will be applied to each field of the sample web form, via its “set_rules()” method, and additionally, specifies the group of values that will be used to repopulate the HTML form, if it’s submitted incorrectly.

Finally, using the previously defined validation rules, the web form is properly checked, and in accordance with the result of this operation, the controller either will redisplay the online form with its fields repopulated, or it’ll simply show a confirmation web page.

At this point, are you starting to realize how easy it is to perform strict validation on a selected web form by using Code Igniter? I guess you are! However, if you’re anything like me, then I’m sure you’ll want to see how each of the respective views are created, right?

So, save the previous controller class to the /system/application/controllers/ folder as “validator.php.” In this way you can test it with your own web server. Assuming that you’ve already done this, click on the link that appears below and keep reading.

{mospagebreak title=Repopulating a web form with Code Igniter}

To be frank, creating the view file responsible for displaying all the errors that might occur when validating the sample web form, and for repopulating its fields when applicable, is a straightforward process that you’ll grasp very quickly.

However, to clarify how this view is going to work, please examine its corresponding signature, which is shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">

<html xmlns="">


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Entering user data</title>



<h1>Entering user data</h1>

<?php echo $this->validation->error_string;?>

<?php echo form_open(‘validator’);?>

<p>First Name <input type="text" name="firstname" value="<?php echo $this->validation->firstname;?>" size="50" /></p>

<p>Last Name <input type="text" name="lastname" value="<?php echo $this->validation->lastname;?>" size="50" /></p>

<p>Email <input type="text" name="email" value="<?php echo $this->validation->email;?>" size="50" /></p>

<p><input type="submit" value="Send Data" /></p>




Definitely, that wasn’t rocket science, was it? As shown before, the above view file is responsible for displaying a simple contact form, which not surprisingly is composed of three text boxes, which come in handy for entering typical data, such as the user’s first and last names, and an email address as well.

Nevertheless, if you have a closer look at the view file in question, then you’ll see that on top of it, the Code Igniter validation class is used to display all of the  errors that might arise when validating the web form. Other than that, there are a few additional PHP statements interspersed with the structural markup, which are used to repopulate the form’s fields and to generate dynamically the URL for its “action” attribute.

Now you have learned how to build a simple view file that not only will be capable of displaying on screen all of the error messages that occurred when validating the sample HTML form, but will refill its fields with the data entered previously by a user.

Therefore, save this file to the Code Igniter /system/application/views/folder as “webform_view.php” and once you’ve done this, please jump forward and read the following section. In that part, I’ll explain how to build the remaining view file, which will be responsible for showing a confirmation message, after the web form has been submitted successfully.

{mospagebreak title=Completing the form validation application}

In the prior section, I demonstrated how simple it is to check data entered into a sample online form by means of the validation class that comes with Code Igniter. As you saw earlier, it’s perfectly possible to repopulate its fields, too, by adding only a few lines of PHP code.

Nonetheless, to finish developing this HTML form validation application, it’s necessary to generate yet another view file. This file will be tasked with displaying on the browser a simple confirmation message once the pertinent form has been submitted successfully.

So, with this in mind, below I included the definition of this brand new file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">

<html xmlns="">


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>User data submitted successfully!</title>



<h1>User data submitted successfully!</h1>

<p><?php echo anchor(‘validator’,'Go and submit the form again’);?></p>




Did you ever think that creating a confirmation view like the one shown above was pretty difficult? Well, fortunately you were wrong! As you can see, the previous view simply will print on screen a typical message indicating the HTML form has been submitted successfully, as you’ve done probably hundreds of times before.

In addition, it’s worthwhile to notice how the “url” helper function provided by Code Igniter is utilized in this specific case, to include a link that points back to the controller. Of course, this could have been done manually, but I left this task to be accomplished by this helper.

Now that you have grasped how the previous view file does its business, save it to the Code Igniter /system/application/views/ folder as “success_view.php” and finally type the following URL into your browser’s address field to test this sample web application:


Here you have it. At this moment, you’re armed with the required background to start developing your own data checking applications with Code Igniter. Try setting other rules for the validation class, and see what happens in each case.

Final thoughts

In this fifth chapter of the series, I explained how to use the validation class that comes bundled with Code Igniter to validate more strictly the data entered into a sample web form. As you saw before, thanks to the functionality provided by this class, the whole process was fairly simple to follow.

In the next part, I’ll show you how to work with Code Igniter’s active record class. In this way you can learn how to select, insert, update and delete database rows without having to get your hands dirty writing SQL statements.

Don’t miss the next article!

Google+ Comments

Google+ Comments