PHP
  Home arrow PHP arrow Page 3 - Developing a Form Validation System wi...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Developing a Form Validation System with the Observer Pattern in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · This second article of the series is aimed at demostrating how an observer object...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT