We have an application where we might have a form structure such as:
Collection has a dynamic number of items at runtime.
What we’re trying to achieve: I want to validate that at least one of the checkboxes in the collection has been selected.
I have a validator (e.g.
AtLeastOneOfTheCheckboxesIsSelectedValidator) currently on the
Collection element which works fine, basically it validates that at least one of the
Checkbox elements in the collection have been checked. We’re building the forms dynamically from a form spec (don’t ask…) so the input filter spec looks like:
| |- my-textarea
| | `- filters
| | |- name = Zend\Filter\StringTrim
| | `- name = Zend\Filter\StripTags
| `- type = Zend\InputFilter\InputFilter
`- name = App\Validator\AtLeastOneOfTheCheckboxesIsSelectedValidator
However, looking at how
createInputFilter work, it seems that the filters added to
my-textarea are never added, because
my-collection is not an
InputFilterInterface (because I didn’t specify that as the
type, so it treats it as an
Input). It seems it can either behave as an
InputFilterInterface (and contain
Inputs) or as an
Input (and have validators/filters), but not both. I only noticed this because we discovered the
StripTags weren’t actually running.
AtLeastOneOfTheCheckboxesIsSelectedValidator gets passed the array of values inside the collection, which means we have access to all the values; the validator loops over the values, identifies which element is the checkbox and when it finds a checked one returns
true early. If it can’t find a checked one, we add a message and return
false (no checkboxes selected).
The problem, in short the
AtLeastOneOfTheCheckboxesIsSelectedValidator on the collection works fine, but the
filters for any elements below the collection are not added due to the way the
Zend\InputFilter\Factory works. I can modify the spec so that the
filters are added properly, but then the
AtLeastOneOfTheCheckboxesIsSelectedValidator does not get added.
Any advice, or ideas on how to make this work, would be appreciated.
We’re using Zend\Form 2.10.0 and Zend\InputFilter 2.7.3 currently (in an Expressive 2.0.2 application on PHP 5.6, but I think that’s largely irrelevant).