PHP
  Home arrow PHP arrow Page 2 - 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? 
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 - Going backward: listing the full source code for the form validation classes
    ( Page 2 of 4 )

    Prior to any attempt to create a true observer object within the form validation application that you learned in the previous article, it would be very convenient to have close at hand the definition of all the data checking classes that I defined previously. In this way, you'll understand more easily how the Observer pattern can be properly implemented inside the application in question. Therefore, here's the complete listing of all the verification classes previously created:

      // 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);
            }
        }
      }
      // define AlphaValidator class
      class AlphaValidator extends DataValidator{
        public function __construct($formObserver){
            parent::__construct($formObserver);
        }
        // validate alphabetic field
        public function validate($field,$errorMessage){
            if(!isset($this->method[$field])||!preg_match("/^[a-zA-Z]
    +$/",$this->method[$field])){
                $this->notifyObserver($errorMessage);
            }
        }
      }
      // define AlphanumValidator class
      class AlphanumValidator extends DataValidator{
        public function __construct($formObserver){
            parent::__construct($formObserver);
        }
        // validate alphanumeric data
        public function validate($field,$errorMessage){
            if(!isset($this->method[$field])||!preg_match("/^[a-zA-
    Z0-9]+$/",$this->method[$field])){
                $this->notifyObserver($errorMessage);
            }
        }
      }
      // define EmailValidator class
      class EmailValidator extends DataValidator{
        public function __construct($formObserver){
            parent::__construct($formObserver);
        }
        // validate email
        public function validate($field,$errorMessage){
            if(!isset($this->method[$field])||!preg_match
    ("/.+@.+..+/",$this->method[$field])||!checkdnsrr(array_pop
    (explode("@",$this->method[$field])),"MX")){
                $this->notifyObserver($errorMessage);
            }
        }
      }
      // define EmailValidatorWin class (Windows systems)
      class EmailValidatorWin extends DataValidator{
        public function __construct($formObserver){
            parent::__construct($formObserver);
        }
        public function validate($field,$errorMessage){
            if(!isset($this->method[$field])||!preg_match
    ("/.+@.+..+/",$this->method[$field])||!$this->windnsrr(array_pop
    (explode("@",$this->method[$field])),"MX")){
                $this->notifyObserver($errorMessage);
            }
        }
        // private method 'windnsrr()' for Windows systems
        private function windnsrr($hostName,$recType=''){
            if(!empty($hostName)){
                if($recType=='')$recType="MX";
                exec("nslookup -type=$recType $hostName",$result);
                foreach($result as $line){
                    if(preg_match("/^$hostName/",$line)){
                        return true;
                    }
                }
                return false;
            }
            return false;
        }
      }

    Here you have them. As you can see above, I listed the complete set of data checking classes that I plan to use with the form validation system, in such a way that they can notify an observer object of any potential errors via the corresponding "notifyObserver()" method. Obviously, this object will determine the best course of action to take, in accordance with the programming logic implemented inside the application.

    Since the above data validation classes should be familiar to you (remember that they were originally defined in the second part of this series), it's time to see how an observer class can be created, in order to decide what tasks to perform, based on the proper notifications sent by the pertinent checking classes.

    Due to the important concepts that I'm going to deploy in the upcoming section, I strongly suggest you keep reading, in order learn how to include an observer class within the previous form validation application.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT