PHP
  Home arrow PHP arrow Page 2 - Validating Web Forms with the Code Ign...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PHP

Validating Web Forms with the Code Igniter PHP Framework
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · As usual with many other PHP frameworks, Code Igniter comes equipped with a useful...
       · Its spelled CodeIgniter not Code Igniter. :))
       · Thanks for catching that. The correct name is CodeIgniter.Regards.
       · Thanks for the articles It is a great help.If the form is submitted with one...
       · Thanks for the comment on my CI article. Regarding your question, you should use the...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT