title: How to use collections in a fieldset factory in ZF2
body: <p>I am developing a project with ZF2 and Doctrine.  I am attempting to use Doctrine Hydrator in the form creation as shown in <a href="https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md#a-complete-example-using-zendform" rel="nofollow noreferrer">this tutorial</a>. In this method, an ObjectManager object is created in the controller and passed to the new form when it is instantiated.  Passing the ObjectManager object from the controller to the form creates a problem when I want to use ZF2's FormElementManager because ZF2 requires that I get an instance of the form class through the Zend\Form\FormElementManager instead of directly instantiating it.  To work around this requirement, I have created form and fieldset factories based upon the answer to the question <a href="https://stackoverflow.com/questions/34551965/how-to-pass-a-doctrine-objectmanager-to-a-form-through-zf2-formelementmanager/34552438?noredirect=1#comment56913198_34552438">How to pass a Doctrine ObjectManager to a form through ZF2 FormElementManager</a>.  The method presented in the answer to the question works for typical fieldset elements, but I need to determine how to include a collection element.  The tutorial uses the ObjectManager object in the collection element in the parent fieldset, and I need to figure out how to add the collection using a factory.</p>
  
  <h3>TagFieldset from the tutorial that I am trying to emulate:</h3>
  
  <pre><code>namespace Application\Form;
  
  use Application\Entity\Tag;
  use Doctrine\Common\Persistence\ObjectManager;
  use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
  use Zend\Form\Fieldset;
  use Zend\InputFilter\InputFilterProviderInterface;
  
  class TagFieldset extends Fieldset implements InputFilterProviderInterface
  {
      public function __construct(ObjectManager $objectManager)
      {
          parent::__construct('tag');
  
          $this-&gt;setHydrator(new DoctrineHydrator($objectManager))
               -&gt;setObject(new Tag());
  
          $this-&gt;add(array(
              'type' =&gt; 'Zend\Form\Element\Hidden',
              'name' =&gt; 'id'
          ));
  
          $this-&gt;add(array(
              'type'    =&gt; 'Zend\Form\Element\Text',
              'name'    =&gt; 'name',
              'options' =&gt; array(
                  'label' =&gt; 'Tag'
              )
          ));
      }
  
      public function getInputFilterSpecification()
      {
          return array(
              'id' =&gt; array(
                  'required' =&gt; false
              ),
  
              'name' =&gt; array(
                  'required' =&gt; true
              )
          );
      }
  }
  </code></pre>
  
  <h3>new TagFieldsetFactory:</h3>
  
  <pre><code>namespace Application\Form;
  
  use Zend\Form\Fieldset;
  use Application\Entity\Tag;
  
  class TagFieldsetFactory
  {
      public function __invoke($formElementManager, $name, $requestedName)
      {
          $serviceManager = $formElementManager-&gt;getServiceLocator();
          $hydrator = $serviceManager-&gt;get('HydratorManager')-&gt;get('DoctrineEntityHydrator');
  
          $fieldset = new Fieldset('tags');
          $fieldset-&gt;setHydrator($hydrator);
          $fieldset-&gt;setObject(new Tag);
  
          //... add fieldset elements.
          $fieldset-&gt;add(['...']);
          //...
  
          return $fieldset;
      }
  }
  </code></pre>
  
  <h3>BlogPostFieldset from the tutorial that I am trying to emulate:</h3>
  
  <pre><code>namespace Application\Form;
  
  use Application\Entity\BlogPost;
  use Doctrine\Common\Persistence\ObjectManager;
  use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
  use Zend\Form\Fieldset;
  use Zend\InputFilter\InputFilterProviderInterface;
  
  class BlogPostFieldset extends Fieldset implements InputFilterProviderInterface
  {
      public function __construct(ObjectManager $objectManager)
      {
          parent::__construct('blog-post');
  
          $this-&gt;setHydrator(new DoctrineHydrator($objectManager))
               -&gt;setObject(new BlogPost());
  
          $this-&gt;add(array(
              'type' =&gt; 'Zend\Form\Element\Text',
              'name' =&gt; 'title'
          ));
  
          $tagFieldset = new TagFieldset($objectManager);
          $this-&gt;add(array(
              'type'    =&gt; 'Zend\Form\Element\Collection',
              'name'    =&gt; 'tags',
              'options' =&gt; array(
                  'count'           =&gt; 2,
                  'target_element' =&gt; $tagFieldset
              )
          ));
      }
  
      public function getInputFilterSpecification()
      {
          return array(
              'title' =&gt; array(
                  'required' =&gt; true
              ),
          );
      }
  }
  </code></pre>
  
  <h3>new BlogPostFieldsetFactory:</h3>
  
  <pre><code>namespace Application\Form;
  
  use Zend\Form\Fieldset;
  use Application\Entity\BlogPost;
  
  class BlogPostFieldsetFactory
  {
      public function __invoke($formElementManager, $name, $requestedName)
      {
          $serviceManager = $formElementManager-&gt;getServiceLocator();
          $hydrator = $serviceManager-&gt;get('HydratorManager')-&gt;get('DoctrineEntityHydrator');
  
          $fieldset = new Fieldset('blog_post');
          $fieldset-&gt;setHydrator($hydrator);
          $fieldset-&gt;setObject(new BlogPost);
  
          //... add fieldset elements.
          $fieldset-&gt;add(['...']);
          //...
  
          return $fieldset;
      }
  }
  </code></pre>
  
  <h3>in module.config.php:</h3>
  
  <pre><code>'form_elements' =&gt; [
      'factories' =&gt; [
          'UpdateBlogPostForm' =&gt; 'Application\Form\UpdateBlogPostFormFactory',
          'BlogPostFieldset' =&gt; 'Application\Form\BlogPostFieldsetFactory',
          'TagFieldset' =&gt; 'Application\Form\TagFieldsetFactory',
      ],
  ],
  </code></pre>
  
  <p>When I add the fieldset elements In my new BlogPostFieldsetFactory I replace this code from the original fieldset:</p>
  
  <pre><code>$this-&gt;add(array(
          'type' =&gt; 'Zend\Form\Element\Text',
          'name' =&gt; 'title'
  ));
  </code></pre>
  
  <p>with this:</p>
  
  <pre><code>$fieldset-&gt;add(array(
          'type' =&gt; 'Zend\Form\Element\Text',
          'name' =&gt; 'title'
  ));
  </code></pre>
  
  <p>How do I replace the collection element from the original fieldset:</p>
  
  <pre><code>$tagFieldset = new TagFieldset($objectManager);
  $this-&gt;add(array(
          'type'    =&gt; 'Zend\Form\Element\Collection',
          'name'    =&gt; 'tags',
          'options' =&gt; array(
                  'count'           =&gt; 2,
                  'target_element' =&gt; $tagFieldset
          )
  ));
  </code></pre>