PHP
  Home arrow PHP arrow Page 2 - Building a Data Validation System with...
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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 Data Validation System with the Prototype Pattern with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2007-05-22

    Table of Contents:
  • Building a Data Validation System with the Prototype Pattern with PHP 5
  • Building a simple data validation system
  • Creating a few additional classes
  • Creating an example

  • 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 Data Validation System with the Prototype Pattern with PHP 5 - Building a simple data validation system


    (Page 2 of 4 )

    As I said right in the beginning of this article, I'm going to demonstrate how to implement the prototype pattern by creating a simple mechanism for validating input data. As you'll see, this data checking application will be capable of working with many instances of a specific object, which will be used to verify the validity of certain data types, such as email addresses, alphanumeric and alphabetic values, etc.

    Okay, having explained how I plan to apply the prototype pattern in this particular situation, please have a look at the signatures of the following prototype classes. They establish a generic structure for validating alphanumeric and alphabetic strings.

    Here are the prototype classes:

    // define abstract 'DataValidatorPrototype' class
    abstract class DataValidatorPrototype{
       abstract public function validateData($inputData);
       abstract public function __clone();

    }

    // define concrete 'AlphabeticValidatorPrototype' class
    class AlphabeticValidatorPrototype extends DataValidatorPrototype{
       public function validateData($inputData){
         if(!$inputData||!preg_match("/^[a-zA-Z]+$/",$inputData)){
           return false;
         }
         return true;
       }
       public function __clone(){}
    }

    // define concrete 'AlphanumericValidatorPrototype' class
    class AlphanumericValidatorPrototype extends DataValidatorPrototype{
       public function validateData($inputData){
         if(!$inputData||!preg_match("/^[a-zA-Z0-9]+$/",$inputData)){
           return false;
         }
         return true;
       }
       public function __clone(){}
    }

    As illustrated above, the three classes are very convenient for checking certain types of data, and come in handy for working with alphabetic and alphanumeric values.

    Of course, you'll realize that the first abstract class, the one called "DataValidatorPrototype," is used to define the generic characteristics of any data validator object that might be potentially derived from it. However, aside from studying in detail the functionality provided by these classes, you should notice that each of them declares a magic "__clone()" method.

    This is very relevant, since the intrinsic definition of the prototype pattern says that all the objects spawned from the prototype will be created via a cloning process. Thus, that's exactly the reason for declaring the "__clone()" method. Quite simple, right?

    All in all, I defined two concrete classes for validating alphabetic and alphanumeric data. Considering that I'd like to extend the initial capacity of this data checking application, in the section to come I'm going to define a few additional prototype classes. They will be tasked with validating numbers and email addresses as well.

    To see how these brand new classes will be built, click on the link below and keep reading. 

    More PHP Articles
    More By Alejandro Gervasio


       · This last article of the series goes through the development of a data validation...
       · First I want to say that I enjoy reading your articles. They are clearly written...
       · Firstly, I’d like to say I’m glad you find my PHP articles useful, so I thank you...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway