PHP
  Home arrow PHP arrow Building a PHP5 Form Processor: Coding...
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

Building a PHP5 Form Processor: Coding the Form Validator Module
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2006-01-23

    Table of Contents:
  • Building a PHP5 Form Processor: Coding the Form Validator Module
  • Checking for empty and integer values: looking at the “validateEmpty()” and “validateInteger()” methods
  • Testing numeric values and ranges: defining the “validateNumber()” and “validateRange()” methods
  • The big challenge: checking email addresses with the “validateEmail()” method
  • Checking for errors: defining the “checkErrors()” and displayErrors()” methods

  • 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


    Building a PHP5 Form Processor: Coding the Form Validator Module


    (Page 1 of 5 )

    In this second part of a three-part series, we take up the topic of server-side validation. By the time you finish this article, you'll have the general guidelines for how to build a form validating class. You'll use some PHP built-in introspection functions, along with regular expressions, to assist you in building this class.

    Introduction

    This is the second part of the series “Building a PHP 5 form processor.” Welcome back. In the first article, I demonstrated how to programmatically construct online forms, in conjunction with the implementation of basic client-side validation. For this reason, I used some extensible PHP 5 classes. This turns the whole form generation process into a straightforward task, which comes in very handy when at least rough client-side verification is first performed on user input, and then server-side validation is applied before processing the data in some way.

    The benefits of having a bunch of classes that do the hard work for constructing web forms rests on the ease of rendering form elements and building their proper layout. You need only use some lines of readable code; you do not need to deal specifically with the form’s (X)HTML markup or JavaScript functions responsible for performing data validation. This situation introduces a greater level of abstraction when building forms, something nearly impossible to achieve with traditional form development methods.

    However, at the time of writing this series, I established that server-side validation would be an important part of the generic form processor package. Keeping this idea in mind, the second part of this series will be directed to developing a PHP 5 form verification class, which can be easily extended and improved either by modifying its existing checking methods or adding additional ones, in order to meet the requirements of a given web application.

    Hopefully, by the end of this tutorial, you’ll have the general guidelines for how to build a form validating class, by utilizing some PHP built-in introspection functions, along with the functionality of regular expressions.

    Want to know more about developing the form “validator” module? Right, stick with me and start reading!

    Basic server-side validation: the barebones of a PHP 5 form validation class

    Before any attempt to write the form “validator” class, first allow me to explain the driving logic hidden behind its structure. Essentially, the class will expose some basic methods for validating form data, ranging from checking for empty fields to more complex data validation, such as verifying email addresses. In addition, I’ll provide the class with the ability to validate alphabetic and alphanumeric data, float and integer values, and finally whether data is within a specific range of values. Of course, since these methods are only a small piece of the data validation terrain, you can write your own checking methods and extend the capabilities of the class, so it can be used in production environments.

    Now that you know how the class will work, take a look at its structure:

    class formValidator{
        private $errors=array();
        public function __construct(){}
        // validate empty field
        public function validateEmpty
    ($field,$errorMessage,$min=4,$max=32){
        }
        // validate integer field
        public function validateInt($field,$errorMessage){
        }
        // validate numeric field
        public function validateNumber($field,$errorMessage){
        }
        // validate if field is within a range
        public function validateRange
    ($field,$errorMessage,$min=1,$max=99){
        }
        // validate alphabetic field
        public function validateAlphabetic($field,$errorMessage){
        }
        // validate alphanumeric field
        public function validateAlphanum($field,$errorMessage){
        }
        // validate email
        public function validateEmail($field,$errorMessage){
        }
        // check for errors
        public function checkErrors(){
        }
        // return errors
        public function displayErrors(){
        }
    }

    As you can see, the structure of the above class shows how it will operate. Originally, the class will expose only one private property, $errors, defined by default as an array structure. As you may have guessed, this property will store the error messages corresponding to each offending field that doesn’t meet the validation rules. Aside from this property, I’ve defined the constructor as an empty method (certainly, you may want to completely remove it from the class). Eventually, it might be tasked with some initializing process, so I’ll keep it that way for now.

    With reference to the validation methods in question, all of them will accept basically two arguments: the name of the form field to be verified, along with the appropriate error message to be displayed. As I said earlier, I’ve defined a few methods for validating empty values, integers, alphanumeric data and so forth, but the versatility of the class allows you to easily add more methods, such a those aimed at checking for URLs, IP addresses, postal codes, and so on.

    Having described the data checking methods originally exposed by the class, there are a couple of methods that remain to be discussed. The first one, “checkErrors()” will be responsible for iterating over the $errors array, determining the existence (or not) of offending form fields. Finally, the “displayErrors()”, as it suggests, will return all the error messages stored in the $errors array to be properly displayed.

    At this stage, I’m sure you can imagine at least the basic logic that will implement the corresponding validation methods, so it’s time to leap forward and explicitly define the signatures for each of them.

    More PHP Articles
    More By Alejandro Gervasio


       · The second article of this series walks through the development of the corresponding...
       · Though in practical application you're not likely to run into this as often, a...
       · First of all, I'd like to thank you for your comments on my article, and secondly...
       · Hi Alejandro,Thanks again for a great article. My compliments.I have a...
       · Hello Matthijs,Thank you for your kind comments on this article. I simply used...
       · hi,thanks for this great article but will the third part be out..eagerly...
       · Isnt this...
       · yep, that it. Thanks you for the link.
       · Hi,Thank you for your kind comments on this tutorial. Your feedback is really...
       · Hey Rich, thanks for providing the correct link to the third article.My Best...
       · Hi,great article(s),but i have a question....the validateEmpty functions checks...
       · Hi,Thank you for commenting on my article. With regard to your question, if you...
       · dont like the way this class relies on the global scope.. namely the _POST...
       · Thank you for your suggestion. In fact, I mentioned this issue in the article too,...
     

       

    PHP ARTICLES

    - 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
    - Database Security: Guarding Against SQL Inje...
    - 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
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





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