Home arrow PHP arrow Page 3 - Developing a Form Validation System with the Observer Pattern in PHP

Extending the scope of data validation: defining some additional data checking classes - PHP

Want to gain a good grounding in how to apply the Observer pattern in PHP? Then you’re in the right place! Welcome to the second part of the series “The Observer Pattern in PHP.” Composed of three tutorials, this series teaches you the key concepts of the popular Observer design pattern, and shows you how to apply it in the context of real-world PHP applications.

TABLE OF CONTENTS:
  1. Developing a Form Validation System with the Observer Pattern in PHP
  2. Validating input data: constructing some useful data validation classes
  3. Extending the scope of data validation: defining some additional data checking classes
  4. Getting the whole picture: listing the full source code for the data checking classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
July 24, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: