Home arrow PHP arrow Page 4 - Centralizing the Validation of Data with the Observer Pattern in PHP

Validating forms: putting the form observer class to work - PHP

Are you looking for an easygoing article that teaches you how to implement the Observer design pattern inside your PHP 5 applications? Your search is finished! Welcome to the last part of the series "The Observer Pattern in PHP." Made up of three articles, this series will show you how to create and work with observer objects, without making you scratch your head while looking at complex code samples.

TABLE OF CONTENTS:
  1. Centralizing the Validation of Data with the Observer Pattern in PHP
  2. Going backward: listing the full source code for the form validation classes
  3. Notifying the application's core level of validation errors: defining a form observer class
  4. Validating forms: putting the form observer class to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
July 31, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Based on the class definitions that you learned over the previous section, below I coded a basic example of how to validate an online form, comprised of the following three text fields: First Name, Last Name and Email. Please take a look at the following code snippet:

  try{
    // instantiate 'FormObserver' object
    $formObs=new FormObserver('send');
    // instantiate validators and validate fields
    $alphaVal=new AlphaValidator($formObs);
    $alphaVal->validate('firstname','Enter a valid First Name
(only alphabetic characters).');
    $alphaVal->validate('lastname','Enter a valid Last Name (only
alphabetic characters).');
    $intVal=new IntegerValidator($formObs);
    $intVal->validate('age','Enter a valid age (0-99).');
    $emailVal=new EmailValidatorWin($formObs);
    $emailVal->validate('email','Enter a valid email address.');
    // check for errors
    $formObs->checkNotifications();
  }
  catch(Exception $e){
            echo $e->getMessage();
            exit();
  }

That's all you need to get the form validation application working. As you can appreciate, first I instantiated a "FormObserver" object, which is naturally passed to each of the data checking classes that will be used during the verification process. Then the form observer checks for any eventual raised errors and either displays a confirmation message (if the data entered on the form is considered valid input), or redisplays the form in question.

To simplify the above explanation even more, here's a screen shot that illustrates how the form validation application works:

Definitely, you'll have to agree with me that the Observer pattern has been quite helpful for constructing an extensible form validation mechanism. Although you may have tried other approaches in the past, this one certainly deserves a closer look, since it shows in a clear way how to apply this design pattern in a real world situation. Give it a try!

Final thoughts

Our journey covering the Observer pattern has now finished. From covering the key concepts, to working with real world examples, I hope that you learned all the topics required to get you off to a good start using this popular pattern. As with other design patterns, practice is the best way to master this one, therefore I strongly encourage you to begin using it (when applicable) inside your own Web applications.

Once you master its fundamentals, fun is guaranteed. See you in the next PHP tutorial!



 
 
>>> 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: