PHP
  Home arrow PHP arrow Page 3 - 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 some strategy classes
    ( Page 3 of 5 )

    As you'll certainly recall from the previous section, the validation strategy selector spawns four different objects, and each one of them is responsible for validating a specific type of data. Therefore, taking this situation into account, below I listed the respective signatures corresponding to these strategy classes.

    Here they are, so have a look at them, please:

    // define 'StrategyAlphabetic' class
    class StrategyAlphabetic{
       public function validateData($inputData){
         if(!$inputData||!preg_match("/^[a-zA-Z]+$/",$inputData)){
           return false;
         }
         return true;
       }
    }
    // define 'StrategyAlphanumeric' class
    class StrategyAlphanumeric{
        public function validateData($inputData){
          if(!$inputData||!preg_match("/^[a-zA-Z0-9]
    +$/",$inputData)){
            return false;
          }
          return true;
        }
    }
    // define 'StrategyNumber' class
    class StrategyNumber{
       public function validateData($inputData){
         if(!$inputData||!is_numeric($inputData)){
           return false;
         }
         return true;
       }
    }
    // define 'StrategyEmail' class
    class StrategyEmail{
       public function validateData($inputData){
         if(!$inputData||!preg_match("/.+@.+..+./",$inputData)||!
    checkdnsrr(array_pop(explode("@",$inputData)),"MX")){
           return false;
         }
         return true;
       }          
    }

    As shown above, each of the four strategy classes listed previously implements differently the same "validateData()" method to check a diverse range of user inputs, such as alphabetic and alphanumeric values, and number and email addresses.

    Of course, it's possible to extend the initial validation capacity of the whole data checking system even more, simply by adding new strategy classes to the existing ones. This is definitely a process that can be performed with minor hassles.

    So far, so good. At this point, you've seen not only how the validation strategy selector looks, but how the four strategy classes have been properly defined. This is very convenient for understanding the schema imposed by the strategy pattern.

    So, the question that comes up is: what's the next step now? Well, considering that you already grasped the logic implemented by all the classes that I defined so far, it's time to move forward and see a concrete example where a data validation system is created by using the aforementioned classes.

    Want to see how the strategy pattern is applied to validate user-supplied input? Jump straight into the following section 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 4 hosted by Hostway
    Stay green...Green IT