PHP
  Home arrow PHP arrow Page 4 - 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 - Validating user-supplied data
    (Page 4 of 5 )

    To demonstrate how all the classes that you learned in previous sections of this tutorial can be used to check the validity of different types of data, below I coded a short script that shows some illustrative cases where the validation strategy selector verifies firstly alphabetic and alphanumeric values, and lastly numbers and email addresses.

    Having said that, the corresponding examples look like this:

    try{
       // example using the 'Strategy pattern'
       $strategyAlpha=new ValidationStrategySelector('alpha');
       echo $strategyAlpha->validateData(123);
       /* displays the following:
       Input data is not valid!
       */
       $strategyAlphanum=new ValidationStrategySelector('alphanum');
       echo $strategyAlphanum->validateData('ABC123');
       /* displays the following
       Input data is OK!
       */
       $strategyNumber=new ValidationStrategySelector('number');
       echo $strategyNumber->validateData('ABC');
       /* displays the following:
       Input data is not valid!
       */
       $strategyEmail=new ValidationStrategySelector('email');
       $strategyEmail->validateData('inexistent-user@domain.com');
       /* displays the following:
       Input data is not valid!
       */         
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As you can see, the prior examples illustrate in a step-by-step format how the strategy design pattern can be used in a real situation, in this particular case, aimed at validating a wide variety of data types.

    As you saw, the first two examples use the validation strategy selector to verify alphabetic and alphanumeric values in turn, and also display the respective results. Finally, the other two cases demonstrate how to use the same selector to validate numeric data and an email address.

    All right, I believe that all these examples should give you an approximate idea of how the strategy pattern can be used in a real-world situation. In the last section of this tutorial I'm going to list all the classes that you learned here. By doing so, you'll have available at one single place the complete source code required to implement the strategy pattern in PHP 5.

    As you know, the last section of this article is just one click away, so go forward and keep reading.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Getting More Info on Reflected Properties wi...
    - Implementing the Iterator SPL Interface
    - Building a Data Access Layer for the Data Ma...
    - Building a Singleton Database with Restricti...
    - Working with Reflected Properties with the R...
    - The Iterator, Countable and ArrayAccess SPL ...
    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



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