title: Zend Framework 2 Doctrine 2 - Multiple Forms same View
body: <p>I have created a ZF2 project with the following dependencies:</p>
  
  <pre><code>    "php": "&gt;=5.3.3",
      "zendframework/zendframework": "2.*",
      "zendframework/zend-developer-tools": "dev-master",
      "doctrine/doctrine-orm-module": "dev-master",
      "bjyoungblood/bjy-profiler": "dev-master"
  </code></pre>
  
  <p>I created a User Entity and a RegisterUser Form.  In my index action I bind the User Entity to  my register form and pass it to my view - like so:</p>
  
  <pre><code>public function indexAction() {
  
      $entityManager = $this-&gt;getServiceLocator ()-&gt;get ( 'Doctrine\ORM\EntityManager' );
  
      $form = new RegisterUserForm ( $this-&gt;serviceLocator );
  
      $user = new User ();
      $form-&gt;bind ( $user );
  
      if ($this-&gt;getRequest()-&gt;isPost()) {
          $form-&gt;setData ( $this-&gt;getRequest()-&gt;getPost () );
          if ($form-&gt;isValid ()) {
              $entityManager-&gt;persist($user);
              $entityManager-&gt;flush();
          }
      }
  
      return new ViewModel(array('form' =&gt; $form));
  }
  </code></pre>
  
  <p>Now I would like to add another form (login) to the same view.  I am having some trouble figuring out the best way to do so.  Binding multiple forms to the same User entity feels dangerous, and I'm not sure how to handle the posts in a different actions.</p>
  
  <p>Here is a look at my form:</p>
  
  <pre><code>class RegisterUserForm extends Form
  {
  public function __construct(ServiceManager $serviceManager)
  {
  
      parent::__construct('register-user-form');
  
      $entityManager = $serviceManager-&gt;get('Doctrine\ORM\EntityManager');
  
      $this-&gt;setHydrator(new DoctrineHydrator ($entityManager, 'Application\Entity\User'));
  
      $userPostFieldset = new UserPostFieldset($serviceManager);
      $userPostFieldset-&gt;setUseAsBaseFieldset(true);
  
      $this-&gt;add($userPostFieldset);
  
  }
  
  }
  </code></pre>
  
  <p>And the corresponding Fieldset:</p>
  
  <pre><code>class UserPostFieldset extends Fieldset implements InputFilterProviderInterface {
  public function __construct(ServiceManager $serviceManager) {
      parent::__construct ( 'user' );
  
      $entityManager = $serviceManager-&gt;get ( 'Doctrine\ORM\EntityManager' );
  
      $this-&gt;setHydrator ( new DoctrineHydrator ( $entityManager, 'Application\Entity\User' ) )-&gt;setObject ( new User () );
  
      $this-&gt;add ( array (
              'type' =&gt; 'Zend\Form\Element\Text',
              'name' =&gt; 'firstName',
              'options' =&gt; array (
                      'label' =&gt; 'First Name'
              )
      ) );
  
      $this-&gt;add ( array (
              'type' =&gt; 'Zend\Form\Element\Email',
              'name' =&gt; 'email',
              'options' =&gt; array (
                      'label' =&gt; 'Email Address'
              )
      ) );
  
      $this-&gt;add ( array (
              'type' =&gt; 'Zend\Form\Element\Email',
              'name' =&gt; 'emailCheck',
              'options' =&gt; array (
                      'label' =&gt; 'Re-Enter Email Address'
              )
      ) );
  
      $this-&gt;add ( array (
              'name' =&gt; 'submit',
              'attributes' =&gt; array (
                      'type' =&gt; 'submit',
                      'value' =&gt; 'Submit'
              )
      ) );
  }
  public function getInputFilterSpecification() {
      return array (
              'email' =&gt; array (
                      'required' =&gt; true,
                      'filters' =&gt; array (
                              array (
                                      'name' =&gt; 'StripTags'
                              ),
                              array (
                                      'name' =&gt; 'StringTrim'
                              )
                      ),
                      'validators' =&gt; array (
                              array (
                                      'name' =&gt; 'Identical',
                                      'options' =&gt; array (
                                              'token' =&gt; 'emailCheck',
                                              'message' =&gt; 'Emails do not match'
                                      )
                              )
                      )
              )
      );
  }
  }
  </code></pre>
  
  <p>Any insight would be appreciated.</p>
  
  <p>Questions:</p>
  
  <p>1 - What is the best practice for adding multiple forms to a view that deal with the same Entity?</p>
  
  <p>2 - How can I break apart my index action so it passes multiple forms to my view, and then map the posts to different actions to perform validation checks? </p>
  
  <p>Thanks,
  D</p>