PHP
  Home arrow PHP arrow Page 2 - Developing a Form Validation System 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

Developing a Form Validation System with the Observer Pattern in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2006-07-24


    Table of Contents:
  • Developing a Form Validation System with the Observer Pattern in PHP
  • Validating input data: constructing some useful data validation classes
  • Extending the scope of data validation: defining some additional data checking classes
  • Getting the whole picture: listing the full source code for the data checking classes

  • 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


    Developing a Form Validation System with the Observer Pattern in PHP - Validating input data: constructing some useful data validation classes
    ( Page 2 of 4 )

    Before I proceed to demonstrate how the Observer pattern can be used inside a data validation application, I’m going to take a logical path and start defining some independent classes, all of them aimed at validating different types of user-supplied input.

    Once these classes have been created, I’ll show you how to implement an observer object that will be able to perform an efficient validation process on the data entered by users. Considering this schema, below I listed the first set of classes that I plan to use inside the data checking application:

      // define DataValidator class

      class DataValidator{
        protected $method;
        protected $formObserver;
        public function __construct(FormObserver $formObserver){
            $this->formObserver=$formObserver;
            $this->method=$_POST;
        }
        protected function notifyObserver($errorMessage){
            $this->formObserver->addNotification($errorMessage);
        }
      }
      // define StringValidator class
      class StringValidator extends DataValidator{
        public function __construct($formObserver){
            parent::__construct($formObserver);
        }
        // validate strings
        public function validate($field,$errorMessage,$min=4,$max=32){
            if(!isset($this->method[$field])||trim($this->method
    [$field])==''||strlen($this->method[$field])<$min||strlen($this-
    >method[$field])>$max){
                $this->notifyObserver($errorMessage);
            }
        }
      }
      // define IntegerValidator class
      class IntegerValidator extends DataValidator{
        public function __construct($formObserver){
            parent::__construct($formObserver);
        }
        // validate integers
        public function validate($field,$errorMessage){
            if(!isset($this->method[$field])||!is_numeric($this-
    >method[$field])||intval($this->method[$field])!=$this->method
    [$field]){
                $this->notifyObserver($errorMessage);
            }
        }
      }
      // define NumberValidator class
      class NumberValidator extends DataValidator{
        public function __construct($formObserver){
            parent::__construct($formObserver);
        }
        // validate numbers
        public function validate($field,$errorMessage){
            if(!isset($this->method[$field])||!is_numeric($this-
    >method[$field])){
                $this->notifyObserver($errorMessage);
            }
        }
      }
      // define RangeValidator class
      class RangeValidator extends DataValidator{
        public function __construct($formObserver){
            parent::__construct($formObserver);
        }
        // validate ranges
        public function validate($field,$errorMessage,$min=1,$max=99){
            if(!isset($this->method[$field])||$this->method[$field]
    <$min||$this->method[$field]>$max){
                $this->notifyObserver($errorMessage);
            }
        }
      }

    Okay, that’s all for the moment about the data checking classes. As you can see above, I defined a highly generic “DataValidator” class, which takes as an input parameter an object of the type “FormObserver” and then assigns it as a class property.

    Please don’t worry about the meaning of this object right now, since I’ll explain later how it will fit into the whole context of the application. Pay strong attention to the additional “notifyObserver()” method, however, which will be responsible for sending the corresponding error messages to the “FormObserver” object whenever a user-supplied input is considered invalid by a particular data checking class.

    As you’ll realize, after defining this base “DataValidator” class, creating a fine-tuned class to validate specific data types is indeed a straightforward process, which is reflected precisely by the definition of these classes.

    With reference to the validation of specific data, you can see that I created a set of classes that check for valid strings, integers, floating numbers, and ranges respectively. In addition, when a given input fails the checking process (no matter what class is used), then the respective “notifyObserver()” method is called, in order to send a notification about the error that happened.

    At this stage, even though I still didn’t show you a single clue about how an observer object can be coded inside the application, quite probably you’ve guessed its driving logic. Yes, you’re correct, when a particular user input is considered not valid by the corresponding class, a notification message will be sent to the observer, and it will decide what action to take. Now, hopefully you’re beginning to see how all these validation objects are decoupled from the application, right?

    All right, now that you have taken a look at the definition of the previous classes, let’s move forward and create some additional ones to tackle the validation of alphabetic and alphanumeric data, as well as the verification of email addresses.

    To see how this will be done, please read the next section.



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