Simplify Form Validation and Reuse, Use Validation Groups

Do you need to use different elements of a Zend Form, in multiple locations? Are you keen to reuse the same form class and avoid code duplication but don’t know how? Come learn about validation groups.


Kicking off 2014, today we’re investigating a simple, yet powerful, aspect of forms - validation groups. This will be a simple tutorial, as it’s not a very complex topic.

If you’re not familiar with validation groups, they do two things:

  1. Encourage Form Reuse
  2. Filter Out Unrequired Data

How? By limiting the number of form elements required to be validated and the form information retrieved with getData().

Say we had a form, which validates a user. Normally such a form would contain fields such as first and last namesemail addressphone numbersaddress, maybe their social security number and so on. Let’s assume we’ve developed the form and tests verify it works properly.

Let’s say that the company’s growing really quickly and wants to build up its email list. Consequently, you’ve been tasked with adding an email signup form to the company website, a lot like the Malt Blue one from MailChimp.

What do you do? You could write another form, but they share some common elements, and an InputFilter class, causing code duplication. Why create it again and give us more to maintain? What do you do?

Enter Validation Groups.

As stated validation groups give us two key benefits:

Firstly form reuse. We can re-use the same form in both instances, and validation will only be required on the fields within the group, even if other fields have been specified as required.

Then there’s filtering out unrequired data. In most forms, there are other fields which are required during validation, but not in post-processing. For example, the submitreset, and cancel buttons. These aid in form usage, but not in processing the information after validation.

When present, we either need to remove them from the retrieved form data, or handle it in the post-processing functions (not desired).

By using a validation group, when getData() is called on the form object, the returned array contains only the fields specified in the validation group.

Quite handy - no? Let’s work through an example.

Code Example

In this gist you can see code for a simple UserRecord class, created using Zend Form Annotations. Note that a number of the fields are required.

Below are three entries in the array which is returned from a standard implementation of getServiceConfig in a Zend Framework module. Let’s look at how these allow us to reuse our form class in a variety of ways.

'Form\Feed\UserRecordForm' => function ($sm) {
    $form = new Form\UserRecordForm();
    $form->setInputFilter(
      new InputFilter\UserRecordInputFilter()
    );
    return $form;
  }

In the example above, we’ve not set a validation group. In this case, the form will validate as we’d expect it to, asking for all the fields specified and returning them all with getData.

NB: It was pointed out on #zftalk on IRC, that getServiceConfig wasn't the recommended place to configure forms. For a better way, have a look at Howto Handle External Form Element Dependencies with FormElementManager
'Form\Feed\EmailSignupFormEmailOnly' => function ($sm) {
    $form = new Form\UserRecordForm();
    $form->setValidationGroup(array('email'));
    $form->setInputFilter(
      new InputFilter\UserRecordInputFilter()
    );
    return $form;
  }

In this example, we can render a form containing only the email address field. During validation, only the rules of this one will be enforced, and only this field will be returned from getData.

'Form\Feed\EmailSignupFormSmall' => function ($sm) {
  $form = new Form\UserRecordForm();
  $form->setValidationGroup(array(
    'email', 'firstName', 'lastName'
  ));
  $form->setInputFilter(
    new InputFilter\UserRecordInputFilter()
  );
  return $form;
}

In this final example, we’ve created an enhanced signup form. As with the last one, the email field will be validated, and we can also show the first and last name fields, validating them as well. getData will return all three, skipping the remaining form fields.

Winding Up

So that’s validation groups. They’re a quick, simple, yet efficient feature of \Zend\Form which allows you to reuse forms and limit the data returned to just what you need.

Over To You

Tell me what you think. Will this one, simple, feature make development of forms simpler and more efficient, requiring less code? Have you already tried it? Share your experience in the comments.


You might also be interested in these tutorials too...

Thu, Jul 11, 2013

Zend Framework 2 - The New HTML5 Form Fields - Part 2

In today’s post, we look at more HTML5 Form fields in Zend Framework 2: Month, Range, Color, Week and Number, as well as element properties and attributes. Come look around more of the great new elements available.

Tue, May 22, 2012

Zend Form Mastery with Zend Config – Part 3 Standard Form & Element Options

– Part 3 Standard Form & Element Options This is a Multi Part Series. Check out the other parts: Part 1 - Custom Filter Paths Part 2 - Core Form Configuration Part 4 - Configuring Zend Validators Options We’ve looked at custom form filters and we’ve looked at the core form configuration. But what about the other form properties? What about: setting an element as readonly an element as required ignoring an element and love them or hate them, what about decorators Element Options Outside of the options that we’ve looked at previously, there are a number of other options that we can consider implementing when configuring a Zend Form.

Fri, Apr 27, 2012

Zend Form Mastery with Zend Config - Part 1 Custom Filter Paths

When you’re working with Zend Form you keep your configuration as much out of code as you can - right? Well, if you’ve been working withZend Form for more than a little while, you know that Zend Config really makes that pretty simple - well, some of the time. In this series we look, comprehensively at how to do it all with Zend_Config.


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus