Centralizing the Validation of Data with the Observer Pattern in PHP - Validating forms: putting the form observer class to work (
Page 4 of 4 )
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!
| | Discuss Centralizing the Validation of Data with the Observer Pattern in PHP | | | | | | |
This last article of the series is focused on getting the form validation system... | | | | | | I have enjoyed all your articles and continue to read them. I tried to implement... | | | | | | Hello Cranium,
Thank you for your commenting on this PHP article. Concerning your... | | | | | | Thank you for replying back so quickly. I changed line 94 to what you showed, but... | | | | | | Hello again Cranium,
The error you're getting it's because the $_POST[$formVar]... | | | | | | Hi Alejandro,
I appreciate all you help on this. I changed the line to the... | | | | | | Hello again Cranium,
It seems that you're having additional problems with the... | | | | | | I have done some testing by watching the flow of the output and printing to screen. ... | | | | | | Hi Cranium again,
As I posted above, it's hard to see what's wrong with your... | | | | | | I sent to the e-mail that is listed when clicking on your name by an article; it is... | | | | | | Hi Scot,
I emailed you the solution.
Regards. | | | | | | Very good series again. Thanks! | | | | | | Hi friend,
Thank you for the kind comments om my PHP article.
Regards. | | | | | | In regards to the problem anonymous had before and your suggestion:
If you... | | | | | | Hello Matthijs agin,
Thank you for your feedback concerning this PHP article. The... | | | | | | Alejandro,
Thank you for authoring content that I would expect to see in a... | | | | | | Hello Larry,
I'd like to thank you for the compliments on my PHP article, as well... | | | | | | >>> Post your comment now! | | | | | |
|
 |