Data Driven Design icon

Get Individual Form Elements Out of Zend Framework 2 Form/Collection

Zend framework 2 has a great feature called collections that allows you to process forms with dynamically added fields. Recently I wanted to use collections but needed to pull out the individual elements so that our frontend developer could integrate them into an existing Bootstrap based layout. Unfortunately there doesn't seem to be any documented way of doing this. After playing around for a few minutes I discovered the correct way to go about this. Use the following from within your view to get the individual elements. If you do this and you are using the Collection template you will also need to include to separately render the collection template.

// Itterate over the collection and grab the elements
foreach($form->get('collectionName')->getIterator() as $fieldset)
{ 
    echo  $this->formRow($fieldset->get('elementName')); 
    echo  $this->formRow($fieldset->get('elementAnotherElementName')); 
}
  
// Render the collection template
echo $this->formCollection()->renderTemplate($form->get('collectionName'));
Get In Touch.
Contact

We proudly serve
the Pacific Northwest