PHP
  Home arrow PHP arrow Building a PHP5 Form Processor: Coding the Form Validator Module
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? 
PHP

Building a PHP5 Form Processor: Coding the Form Validator Module
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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