Strategy pattern for form validation?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Strategy pattern for form validation?

Post by matthijs »

Form validation is an often repeated process an can be very tedious to write and control everything again and again.
So encapsulating the validation in a reusable and extendable class seems like a logical step. I have seen and used a couple of classes so far. There are very basic classes which check for just a couple of things and there are very large classes which also do the form building (for example Manuel Lamos' class http://www.phpclasses.org/browse/package/1.html).
On this forum d11wtq shows a class viewtopic.php?t=34883 which is quite complete but easy to use.

Now I want to write my own class. I know that's reinventing the wheel but I hope to learn a thing or two in the process and secondly to have something which fits my needs specifically.
In a couple of places I have read about the Strategy pattern and how it it is very well suited to be used for form validation. See for example http://www.phppatterns.com/docs/design/strategy_pattern and http://www.tonymarston.net/php-mysql/de ... l#strategy.

My guess is that for a quick job, putting together a class with a few basic methods is the best option. However, for the most flexibility and reusability building a set of classes using the Strategy pattern seems a good choice.

What are your thoughts about this? Are there any drawbacks with using the Strategy pattern?

Thanks for your input,

Matthijs
Post Reply