Home arrow PHP arrow Page 2 - Performing Strict Validation with the Code Igniter PHP Framework

Performing strict validation on user-provided data with Code Igniter - PHP

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.

TABLE OF CONTENTS:
  1. Performing Strict Validation with the Code Igniter PHP Framework
  2. Performing strict validation on user-provided data with Code Igniter
  3. Repopulating a web form with Code Igniter
  4. Completing the form validation application
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
September 10, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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

parent::Controller();

// load 'url' helper

$this->load->helper('url');

// load 'form' helper

$this->load->helper('form');

// load 'validation' class

$this->load->library('validation');

}

function index(){

// set validation rules

$rules['firstname']="required|min_length[6]|max_length[15]";

$rules['lastname']="required|min_length[6]|max_length[15]";

$rules['email']="required|valid_email";

$this->validation->set_rules($rules);

// set values for repopulating fields

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

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

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

$this->validation->set_fields($fields);

// check if user form has been submitted properly

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

// redisplay user form and repopulate fields

$this->load->view('webform_view');

}

// display success web page

else{

$this->load->view('success_view');

}

}

}


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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: