Home arrow PHP arrow Page 3 - Validating Incoming Data with the Strategy Design Pattern

Implementing the Strategy design pattern to validate incoming data - PHP

If you’ve been doing some research lately on the web about how to implement different design patterns, be it in Java, PHP or Python, you'll probably have noticed that the Strategy pattern is one of the most popular contenders in the field. It has been covered countless times in all types of media, ranging from books and magazines to e-zines and personal blogs. In this article series, I'll show you how to use this popular design pattern to accomplish one of the more common tasks a website must perform: validating user-supplied data.

  1. Validating Incoming Data with the Strategy Design Pattern
  2. Building a basic data validation class with PHP
  3. Implementing the Strategy design pattern to validate incoming data
  4. Finish defining the form helper class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
April 14, 2010

print this article



As I said, it’s feasible to build a modular and flexible data validation application by taking advantage of the functionality offered by the Strategy pattern. To do so, I'll start by defining a simple web form helper. It will take as an input argument a validator object (the strategy), whose originating class will be shown in upcoming articles of this series.

For the moment, however, focus your attention on the definition of the helper, which looks like this:



class FormHelper


    protected $_validators = array();

    protected $_errors = array();


    // add a validator

    public function addValidator(AbstractValidator $validator)


        $this->_validators[] = $validator;

        return $this;



    // get all the validators

    public function getValidators()


        return !empty($this->_validators) ? $this->_validators : null;



    // validate inputted data

    public function validate()


        $validators = $this->getValidators();

        if (null !== $validators)


            foreach ($validators as $validator)


                if (!$validator->validate())


                    $this->_errors[] = $validator->getFormattedError();




        return empty($this->_errors) ? true : false;


As you can see, the above “FormHelper” class implements for now three concrete methods. The first one, called “addValidator(),” permits you to inject a set of independent validation objects, which are stored on the protected $_validators” property. This method is actually the “heart and soul” of the Strategy pattern, as it uses the power of Composition to inject a predefined validation strategy inside the helper class.

On the other hand, the “validate()” method first iterates over all the inputted validator objects, uses them to check if the supplied data is valid, and finally stores the corresponding error messages by calling the “getFormattedError()” method defined by the objects in question. Naturally, you’ll understand the implementation of this method better when I show you the definition of each validation class, but keep in mind that this is a work in progress, so for the moment keep your attention focused on how the helper class does its thing.

Even though at this point the helper class looks pretty skeletal, it’s clear to see that it can use a specific strategy, that is one or multiple validator objects, to check incoming data. Now, are you starting to see how useful the Strategy pattern can be when building flexible applications? I guess you do!

However, in its current state the helper still isn’t capable of retrieving and removing the error messages stored during the entire validation process. Fortunately, this can be solved by adding a couple of additional methods to it. Their definitions will be shown in the next section.

Now, to see how these extra methods will be defined, click on the link below and keep reading.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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