PHP
  Home arrow PHP arrow Page 4 - 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 - Getting the whole picture: listing the full source code for the data checking classes
    ( Page 4 of 4 )

    Culled from my own experience, I know that it’s much better to have all the source code listed in one place. For this simple reason, below I’ve listed all the data validation classes that I’ve defined in the previous section, so you can have a clear idea of how all the pieces fit together. Here they are:

      // 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 it. Now that you have the entire set of data validation classes listed in one place, feel free to improve them, in accordance with your specific programming requirements. It’s truly educational, believe me.

    To wrap up

    In this second article, you learned how to build a data validation system which uses independent objects for checking the validity of a broad range of user-supplied input, and also is capable of sending information about errors to a core object when user data fails to pass the verification process. However, the application is still incomplete, since the “core object” that I mentioned before remains undefined.

    But, there’s no reason to panic. In the last article, I’ll show you how to define an observer object at the application’s core level, in this way providing the program with a centralized mechanism for making programmatic decisions based upon the errors that occurred when validating user-provided data. I'll meet you in the last 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek