An HTML <form>
post request may looks like:
$post = [
'idUser' => 1,
'address' => 'foo street',
];
You can easily create a Zend Form for this, filter and validate the user input. But what if you have to validate a list of users? Something more like:
$post = [
[
'idUser' => 1,
'address' => 'foo street',
],
[
'idUser' => 2,
'address' => 'bar street',
],
];
There is no name you could use to tell Zend Form how to validate the input. Thats where the Zend Collection Element comes in handy.
UserAddressesForm
-> CollectionElement
-> UserFieldset
-> idUser
-> (may other elements/fieldsets here)
-> CollectionElement
-> AddressesFieldset
-> idAddress
-> street
-> (may other elements/fieldsets here)
Pay attention to
- the CollectionElement within the UserAddressesForm and UserFieldset. It is the only element in this form because of the form of the post request data.
- the validation group within UserAddressesForm::getValidationGroup()
- how to set the collections name within the controller:
$form->setData(['collection' => $request->getPost()]);
For more information, have a look at the Zend Form Documentation Example