Home arrow PHP arrow Page 2 - Validating Web Forms with the Code Igniter PHP Framework

Validating user-supplied data using the MVC pattern - PHP

If you're a web developer, you've probably written any number of scripts designed to validate the input entered into a web form. More than likely, then, you'd like something that will make that job easier. Enter the Code Igniter PHP framework. In this fourth part of a nine-part series on this framework, you'll learn how to use it to do basic validation on a web form.

  1. Validating Web Forms with the Code Igniter PHP Framework
  2. Validating user-supplied data using the MVC pattern
  3. Displaying error messages when validating an online form
  4. Building a confirmation view file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 18
September 03, 2008

print this article



As I mentioned in the introduction, validating web forms with Code Igniter is actually a no-brainer process that can be tackled by any developer with an intermediate background in the Model-View-Controller pattern.

To be frank, the whole validation procedure is reduced to creating a controller class that checks to see if the data collected with an online form is valid or not, and then shows one of two different views, according to the result of this checking process.

Obviously, the first view will display a successful web page, while the second one will show an error message along with the corresponding web form. Quite simple to understand, right?

However, the best way to grasp how to validate online forms with Code Igniter is simply by showing some functional code. Period. Thus, based on this concept, below I listed the signature of a brand new controller class which performs all the validation steps described previously. 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





// check if form has been submitted properly

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

// redisplay web form



// display success web page






It’s hard to believe, but the above “Validator” controller class is almost all the PHP code required to validate a web form. In this specific case, the controller begins loading all of the classes that it’ll need to perform a basic validation on a sample contact form. The form is composed of some typical fields, such as the user’s first and last names, and the email address.

As you can see, the class includes a couple of helper functions, called “url” and “form” respectively. Not surprisingly, these functions are used by Code Igniter to generate dynamic URLs and form parts too. However, these functions will be utilized at a later time, when rendering the corresponding views. Thus, for the moment, pay attention to the following line of code:


As with other examples developed earlier, this expression is used to include the validation class, which naturally is utilized to check whether or not the fields of the sample web form have been filled with data. This process can be more clearly understood if you examine the code snippet below:

// set validation rules





In this concrete situation, the set of checking rules assigned to the validation class are pretty simple actually, since they’ll only verify if the text boxes of the web form aren’t empty. That’s all. However, it’s perfectly possible to specify more strict validation rules, but this topic will be covered in detail in upcoming articles of the series.

Once the controller determines if the web form has been submitted correctly (or not), it’ll redisplay the form, including an error message, or it’ll show a success web page, depending on the result of this validation process. I told you that validating online forms with Code Igniter was really easy! And before I forget, please save this file to the Code Igniter /system/application/controllers/ folder as “validator.php.”

Now that you have hopefully grasped how the previous “Validator” class works, it’s time to see how to create the view file that redisplays the web form when it contains one or more empty fields.

To see how this file will be built, please jump ahead and read the following section.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: