How to use collections in a fieldset factory in ZF2
maybe i'm getting your question wrong.... but if you replaced this
$this->add(array( 'type' => 'Zend\Form\Element\Text', 'name' => 'title'));
whith this:
$fieldset->add(array( 'type' => 'Zend\Form\Element\Text', 'name' => 'title'));
then you probably can replace this:
$tagFieldset = new TagFieldset($objectManager);$this->add(array( 'type' => 'Zend\Form\Element\Collection', 'name' => 'tags', 'options' => array( 'count' => 2, 'target_element' => $tagFieldset )));
with this:
$tagFieldset = new TagFieldset($objectManager);$fieldset->add(array( 'type' => 'Zend\Form\Element\Collection', 'name' => 'tags', 'options' => array( 'count' => 2, 'target_element' => $tagFieldset )));
now, if you cant pass the $objectManger to the form... well if you look at the code you have this thing available $serviceManager, that thing looks like a DI container, im sure you can get the $objectManager instance from there, and if is not available, you can probably put an instance of it inside.
So de final code probably ending looks like this:
$objectManager = $serviceManager->get('DoctrineObjectManager') //or something like this$tagFieldset = new TagFieldset($objectManager);$fieldset->add(array( 'type' => 'Zend\Form\Element\Collection', 'name' => 'tags', 'options' => array( 'count' => 2, 'target_element' => $tagFieldset )));