PHP
  Home arrow PHP arrow Page 3 - 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 - Extending the scope of data validation: defining some additional data checking classes
    ( Page 3 of 4 )

    As I mentioned before, I was planning to extend the validation capabilities of the application. So my next task is to code some new classes, aimed at checking alphabetic and alphanumeric values, along with email addresses. Please take a look at the classes listed below:

      // 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;
        }
      }

    As shown above, I defined an additional set of data checking classes, in order to extend the capabilities of the application. It’s easy to see how these new classes have been created, since they’re also sub classes of the base “DataValidator” class.

    Of course, since I wish to expand the functionality of the whole application, I covered the validation of alphanumeric and alphabetic values, together with the verification of email addresses, in two flavors: Unix-based and Windows systems.

    At this stage, I provided this data-checking application with a decent number of checking classes. These classes are not only independent, but also capable of notifying a core object of any errors that occurred during the checking process. Definitely, you’ll agree with me that I’m on the right track for implementing the Observer pattern with PHP 5. Don’t you feel a little happier?

    But, let’s get serious now and jump into the next section, so you can see the full source code of all the data checking classes that I defined before. Just go ahead and keep on reading.



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