PHP
  Home arrow PHP arrow Page 2 - Validating User Input with the Strategy Pattern
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

Validating User Input with the Strategy Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2007-03-06


    Table of Contents:
  • Validating User Input with the Strategy Pattern
  • Building a validation strategy selector
  • Building some strategy classes
  • Validating user-supplied data
  • Listing all the classes required to implement the strategy pattern

  • 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 User Input with the Strategy Pattern - Building a validation strategy selector
    ( Page 2 of 5 )

    In consonance with the concepts that I expressed in the beginning, my objective here is to demonstrate how the strategy pattern can help to create a data validation system capable of verifying different types of user data.

    Bearing in mind this important requisite, the first step that I'm going to take will consist of building a validation strategy selector (naturally, represented by a PHP class) which will determine what validation strategy must be applied, in accordance with the type of data submitted by a user.

    That being said, the validation strategy selector looks like this:

    // define 'ValidationStrategySelector' class
    class ValidationStrategySelector{
       private $strategy=NULL;
       public function __construct($strategy){
         if($strategy!='alpha'&&$strategy!='alphanum'&&$strategy!
    ='number'&&$strategy!='email'){
           throw new Exception('Invalid parameter for validation
    strategy!');
         }
         switch($strategy){
           case "alpha": 
             $this->strategy=new StrategyAlphabetic();
             break;
           case "alphanum": 
             $this->strategy=new StrategyAlphanumeric();
             break;
           case "number": 
             $this->strategy=new StrategyNumber();
             break;
           case "email": 
             $this->strategy=new StrategyEmail();
             break;
         }
       }
       public function validateData($inputData){
         if(!$this->strategy->validateData($inputData)){
           return 'Input data is not valid!';
         }
         return 'Input data is OK!';
       }
    }

    As you can see, the above strategy selector accepts the type of validation strategy, that is the "$strategy" parameter, as its unique input parameter. Next, according to the value of this variable, a specific strategy class is properly instantiated to validate a huge variety of data types, including alphabetic and alphanumeric data, as well as numbers and email addresses.

    Actually, the logic that stands behind the strategy pattern is demonstrated by the other method that belongs to the previous class, in this case the one called "validateData()," since it takes care of using the proper strategy to validate a specific type of data. So far, the signature of the above class look really simple, right?

    Okay, at this stage I'm sure that you understand how the prior validation strategy selector works. Nevertheless, as you saw, the class in question creates four different strategy objects in turn, in accordance with the type of data being verified.

    Therefore, it's obviously necessary to show the respective signatures for these strategy classes, so you can see more clearly how they're capable of performing different validation on user-supplied data.

    As you may have guessed, defining all these new classes is something that will be covered in the following section. So please click on the link that appears below and keep reading.



     
     
    >>> 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 1 hosted by Hostway
    Stay green...Green IT