Complex input filters for hierarchial data (without Zend\Form)

#1

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.

#2

Not sure how the peeps from ZF feel about it, by I created a repo with a giant ReadMe. It’s focused on the combination of ZF with Doctrine and focuses on re-usable components.

Hopefully the ReadMe there can help you out :wink: If you like, dive a bit into the code, especially the config and the Factory classes provided to get your own ideas.


Posting this as this question is a few days old now. If it’s not appreciated that I post with this link, please let me know and I’ll remove it. Hopefully it can help out davidmintz or others though.

#3

@rkeet thanks. I came across your repo during my research and gave it a look, but was too lazy/impatient to dive deep. I am dealing with my case by hand, with help from some standard ZF filter and validator classes.

I should go back to your thing and look again.