HomePHP 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.
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.