# How to handle initializing and populating forms in Expressive?

**URL:** <https://discourse.laminas.dev/t/how-to-handle-initializing-and-populating-forms-in-expressive/397>\
**Category:** Mezzio\
**Created:** [December 8, 2017, 9:30pm UTC](https://discourse.laminas.dev/t/how-to-handle-initializing-and-populating-forms-in-expressive/397 "2017-12-08T21:30:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dennis-fedco](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/dennis-fedco/32/174_2.png) [@dennis-fedco](https://discourse.laminas.dev/u/dennis-fedco)\
**Post date:** [December 8, 2017, 9:30pm UTC](https://discourse.laminas.dev/t/how-to-handle-initializing-and-populating-forms-in-expressive/397/1 "2017-12-08T21:30:36Z")

</div>

I keep coming across the same issue, where my `action` (aka end-point middleware) needs to use a form, and the form must be populated with values before it is used. By populating I mean both filling out the select elements with any default options, and also setting any default values and setting any choices as `selected`.

So far what I am doing is this - in my action factory I instantiate the form and also initialize the “population service” for the form. I inject both of those classes into my middleware action’s constructor. The inside the action I then use the form service to populate the form.

It feels correct in a sense that I am using DI and SRP but also it feels unnecessarily bloated -\> if I have two forms, do I inject 4 classes into the action? (2 form population services and two forms)?

I am part debating that to reduce the bloat, I may wish to instantiate forms inside the action instead of at service creation time. And if I perhaps want to only have one service to populate all forms, no matter what those forms are.

But before I go inventing my own way of doing this, is there an established pattern to handle this issue?  
P.S. I am using `Zend\Form` in my case.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [December 9, 2017, 8:27am UTC](https://discourse.laminas.dev/t/how-to-handle-initializing-and-populating-forms-in-expressive/397/2 "2017-12-09T08:27:49Z")

</div>

> [@dennis-fedco](#):
>
> if I have two forms, do I inject 4 classes into the action? (2 form population services and two forms)?

- use factories for the forms, which uses your “form population services”, then forms are ready
- add the `FormElementManager` to your action classes and pull the forms from the manager

Here can you find some infos to the `FormElementManager`: [Advanced - zend-form - Zend Framework Docs](https://docs.zendframework.com/zend-form/advanced/)

(If you use a factory for a form, maybe you can discard the related “population service”.)
