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? 
Google.com  
PHP

Validating User Input with the Strategy Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    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

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek