I know the docs say you can “create not only Input
instances, but also nested InputFilter
s, allowing you to create validation and filtering rules for hierarchical data sets,” but it doesn’t exactly show you how. I usually use forms and fieldsets, and muddle through by implementing getInputFilterSpecification()
and then $form->isValid()
. Here I have a case where I need to validate $_POST to assemble an email message, but there’s no form. The input is expected to look like
Array (
[message] => Array
(
[event_details] => Array
(
[date] => 05-Apr-2019
[time] => 2:30 pm
[location] => 706, 40 Foley
// etc
)
[recipients] => Array
(
[to] => Array
(
[0] => Array
(
[email] => somebody@yahoo.com
[name] => Somebody
)
[1] => Array
(
[email] => somebody_else@aol.com
[name] => Somebody Else
)
)
[cc] => Array
(
[0] => Array
(
[email] => another_person@nysd.uscourts.gov
[name] => Another Person
)
)
)
[subject] => some verbiage
[body] => more verbiage
)
[csrf] => 46a3490333698ad44815f5facc8e17f2-bfb5b7a06d21d666863f4ff7df1ddd93
)
As you can see, the key recipients
is an array containing other arrays. The nested to
has to be required (contain at least one element) and each of its elements needs to have its email
validated, while name
is optional, and so forth.
So for now I’ve been experimenting within a controller action, trying to throw a multidimensional array at Zend\InputFilter\Factory
, something like
$data = $this->params()->fromPost('message');
$factory = new \Zend\InputFilter\Factory();
$inputFilter = $factory->createInputFilter([
'recipients' => [
'to' => [
'type'=> 'Zend\InputFilter\CollectionInputFilter',
'required' => true,
// no clue what comes next
]
]
]);
$inputFilter->setData($data);
if ($inputFilter->isValid()) { /* ..... */}
but that’s not going to get it done. Any suggestions, examples, recommended reading?
[update: I’ve flattened out this incoming data structure somewhat by moving the stuff in $message['recipients']
up one level and getting rid of the key recipients
, but the basic problem is the same.]
Thanks.