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{ 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!
blog comments powered by Disqus |