Developing software in the Real World

Exclude elements from Zend\Form's getData()

If you need to exclude some elements from a Zend\Form‘s getData(), the easiest way is to use a validation group.

For example:

The call to setValidationGroup() contains an array of all the elements you want to be validated. In this case we list all elements except the submit button. The side-effect is that when you called $form->getData(), only the values for those elements are returned which can sometimes be useful.


Rather than having to re-list all the elements again, it’s more usually to only want to exclude one or two elements. The easiest way to do this is to extend Zend\Form\Form and override isValid() to set up a validation group based on an option called 'exclude' that you apply to the relevant elements:

Then, to exclude an element, we extend SomeForm from \My\Form and just add a new 'exclude' option to the elements we wish to exclude. For example:

and this element will be automatically excluded when you call $form->getData().