PHP
  Home arrow PHP arrow Page 2 - Performing Strict Validation with the Code Igniter PHP Framework
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Performing Strict Validation with the Code Igniter PHP Framework
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-09-10


    Table of Contents:
  • Performing Strict Validation with the Code Igniter PHP Framework
  • Performing strict validation on user-provided data with Code Igniter
  • Repopulating a web form with Code Igniter
  • Completing the form validation application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Performing Strict Validation with the Code Igniter PHP Framework - Performing strict validation on user-provided data with Code Igniter
    ( Page 2 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek