How to combine multiple annotated entity derived forms into a single form.

So you’re using the data mapper pattern, just for awesomeness. You’re also using annotated entities being parsed into forms, since in most cases your entities are in a one to one relationship with your forms.

However you do occasionally need forms that are made up of multiple entities.

you could create a new entity with all the bits in and then parse them into seperate entities for persisting.

However it’s easier to create a new entity using multiple @Annotation/ComposedObject annotations, this gives us unified validation and can get the seperate entities easily from the outer entity.

Here we have the factory for creating the form. Form Factory:

 1 <?php
 2 
 3     'WelcomeMessageForm' => function ($sm) {
 4 
 5         $msg = new Model\WelcomeMessage;
 6 
 7         $fc =  new Model\LicenceContent;
 8         $msg->setFutureMessage($fc);
 9 
10         $cc =  new Model\LicenceContent;
11         $msg->setCurrentMessage($cc);
12 
13         $builder = new \Zend\Form\Annotation\AnnotationBuilder;
14         $form = $builder->createForm($msg);
15         $form->bind($msg);
16 
17         $form->get('current_message')->setObject(new Model\LicenceContent);
18         $form->get('future_message')->setObject(new Model\LicenceContent);
19         return $form;
20     },

When using ComposedObject, the composed objects are parsed into fieldsets within the form. By doing the setObject calls on the fieldsets in the factory, this allows the form to iterate through the composed objects in a ‘populated’ main entity to populate values in the form.

WelcomeMessage Entity:

 1 <?php
 2 
 3     namespace Module\Model;
 4     use Zend\Form\Annotation;
 5     /**
 6      * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ClassMethods")
 7      */
 8     class WelcomeMessage
 9     {
10 
11         /**
12          * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ClassMethods")
13          * @Annotation\ComposedObject("Module\Model\LicenceContent")
14          */
15         protected $current_message;
16 
17         /**
18          * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ClassMethods")
19          * @Annotation\ComposedObject("Module\Model\LicenceContent")
20          */
21         protected $future_message;
22 
23         public function getCurrentMessage()
24         {
25             return $this->current_message;
26         }
27 
28         public function setCurrentMessage($currentMessage)
29         {
30             $this->current_message = $currentMessage;
31             return $this;
32         }
33 
34         public function getFutureMessage()
35         {
36             return $this->future_message;
37         }
38 
39         public function setFutureMessage($futureMessage)
40         {
41             $this->future_message = $futureMessage;
42             return $this;
43         }
44 
45     }

Here we have a simple ‘super’ entity composed of 2 other objects (in this example, both the same type).

Controller Action:

 1 <?php
 2 
 3     public function welcomeAction()
 4     {
 5         //get id from session
 6         $licence_id = $this->zfcUserAuthentication()->getIdentity()->getId();
 7         $viewModel = new ViewModel();
 8         $form      = $this->getCustomiseService()->getWelcomeMessageForm();
 9         if ($this->getRequest()->isPost()) {
10             $data = $this->params()->fromPost();
11             $form->setData($data);
12             if ($form->isValid()) {
13                 /* @var $entity \Module\Model\WelcomeMessage */
14                 $entity = $form->getData();
15 
16                 /* @var $curMsg \Module\Model\LicenceContent */
17                 $curMsg = $entity->getCurrentMessage();
18 
19                 /* @var $ftrMsg \Module\Model\LicenceContent */
20                 $ftrMsg = $entity->getFutureMessage();
21 
22                 $this->getCustomiseService()->updateWelcomeMessage($licence_id, $curMsg, $ftrMsg);
23 
24             }
25         }
26         $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods);
27         $entity = $this->getCustomiseService()->getWelcomeMessage($licence_id);
28         $form->bind($entity);
29         $viewModel->setVariable('form', $form);
30         return $viewModel;
31     }

Here we have a simple action for validating and saving a form, and for also fetching a ‘populated’ entity from the db.

And that’s it, we have a main object made out of 2 composed objects, the 2 composed objects are parsed into fieldsets in the main form. You can retrieve and set/populate the values of the composed objects easily.