PHP
  Home arrow PHP arrow Page 4 - Centralizing the Validation of Data with the Observer Pattern in PHP
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

Centralizing the Validation of Data with the Observer Pattern in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2006-07-31


    Table of Contents:
  • Centralizing the Validation of Data with the Observer Pattern in PHP
  • Going backward: listing the full source code for the form validation classes
  • Notifying the application's core level of validation errors: defining a form observer class
  • Validating forms: putting the form observer class to work

  • 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


    Centralizing the Validation of Data with the Observer Pattern in PHP - Validating forms: putting the form observer class to work
    ( Page 4 of 4 )

    Based on the class definitions that you learned over the previous section, below I coded a basic example of how to validate an online form, comprised of the following three text fields: First Name, Last Name and Email. Please take a look at the following code snippet:

      try{
        // instantiate 'FormObserver' object
        $formObs=new FormObserver('send');
        // instantiate validators and validate fields
        $alphaVal=new AlphaValidator($formObs);
        $alphaVal->validate('firstname','Enter a valid First Name
    (only alphabetic characters).');
        $alphaVal->validate('lastname','Enter a valid Last Name (only
    alphabetic characters).');
        $intVal=new IntegerValidator($formObs);
        $intVal->validate('age','Enter a valid age (0-99).');
        $emailVal=new EmailValidatorWin($formObs);
        $emailVal->validate('email','Enter a valid email address.');
        // check for errors
        $formObs->checkNotifications();
      }
      catch(Exception $e){
                echo $e->getMessage();
                exit();
      }

    That's all you need to get the form validation application working. As you can appreciate, first I instantiated a "FormObserver" object, which is naturally passed to each of the data checking classes that will be used during the verification process. Then the form observer checks for any eventual raised errors and either displays a confirmation message (if the data entered on the form is considered valid input), or redisplays the form in question.

    To simplify the above explanation even more, here's a screen shot that illustrates how the form validation application works:

    Definitely, you'll have to agree with me that the Observer pattern has been quite helpful for constructing an extensible form validation mechanism. Although you may have tried other approaches in the past, this one certainly deserves a closer look, since it shows in a clear way how to apply this design pattern in a real world situation. Give it a try!

    Final thoughts

    Our journey covering the Observer pattern has now finished. From covering the key concepts, to working with real world examples, I hope that you learned all the topics required to get you off to a good start using this popular pattern. As with other design patterns, practice is the best way to master this one, therefore I strongly encourage you to begin using it (when applicable) inside your own Web applications.

    Once you master its fundamentals, fun is guaranteed. See you in the next PHP tutorial!



     
     
    >>> 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek