PHP
  Home arrow PHP arrow Page 2 - Validating Web Forms 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

Validating Web Forms with the Code Igniter PHP Framework
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2008-09-03


    Table of Contents:
  • Validating Web Forms with the Code Igniter PHP Framework
  • Validating user-supplied data using the MVC pattern
  • Displaying error messages when validating an online form
  • Building a confirmation view file

  • 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


    Validating Web Forms with the Code Igniter PHP Framework - Validating user-supplied data using the MVC pattern
    ( Page 2 of 4 )

    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

    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";

    $rules['lastname']="required";

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

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

    // check if form has been submitted properly

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

    // redisplay web form

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

    }

    // display success web page

    else{

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

    }

    }

    }


    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:

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

    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

    $rules['firstname']="required";

    $rules['lastname']="required";

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

    $this->validation->set_rules($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
     

       

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek