# Populate Select element in form

**URL:** <https://discourse.laminas.dev/t/populate-select-element-in-form/2064>\
**Category:** Components & MVC\
**Tags:** laminas-form, laminas-mvc\
**Created:** [January 22, 2021, 7:07pm UTC](https://discourse.laminas.dev/t/populate-select-element-in-form/2064 "2021-01-22T19:07:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebuddy](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ebuddy/32/1536_2.png) [@ebuddy](https://discourse.laminas.dev/u/ebuddy)\
**Post date:** [January 22, 2021, 7:07pm UTC](https://discourse.laminas.dev/t/populate-select-element-in-form/2064/1 "2021-01-22T19:07:30Z")

</div>

Hi everyone,  
I would like to build a form with select elements and populate them with database entries.  
How can I include many repositories (I have more than one select element) in my form ?  
Is it good to pass the service manager (to access to all my repositories) in the constructor of my forms ?  
Any advice or code snippet ?

---

<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:** [January 23, 2021, 9:33pm UTC](https://discourse.laminas.dev/t/populate-select-element-in-form/2064/2 "2021-01-23T21:33:10Z")

</div>

> [@ebuddy](#):
>
> How can I include many repositories (I have more than one select element) in my form ?

Create separated form elements and you do not need to inject _many_ repositories.  
See also:

> [@Handling Laminas Forms with Dependencies - the view results](https://discourse.laminas.dev/t/handling-laminas-forms-with-dependencies-the-view-results/1343/12):
>
> If only the select element needs entries from a database then you can create a separate form element with the required dependencies. Create the element: class CountrySelectElement extends Laminas\Form\Element\Select { // … public function init() { $this-\>setName('country\_id'); $this-\>setLabel('Countries'); $this-\>setValueOptions($categories-\>fetchAllCountries()); // … } } Create a factory for the element. Register the new element: retur…

> [@ebuddy](#):
>
> Is it good to pass the service manager (to access to all my repositories) in the constructor of my forms ?

See my comment to the same topic here:

> never inject the entire service manager in a class. If you do that, then you have a design fault from the start and you are on the best way to an anti-pattern. Your class should not even know that there is a dependency injection container. Give only what is necessary.

> [@Blog post: Laminas Event Manager and Lazy listeners](https://discourse.laminas.dev/t/blog-post-laminas-event-manager-and-lazy-listeners/2029):
>
> Post about Laminas Event Manager with some examples of implementations. Any feedback is appreciated. [Laminas Event Manager and Lazy listeners](https://www.we-rc.com/blog/2020/11/11/laminas-event-manager-and-lazy-listeners)

---

<div class="post-metadata">

**Author:** ![ebuddy](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ebuddy/32/1536_2.png) [@ebuddy](https://discourse.laminas.dev/u/ebuddy)\
**Post date:** [January 24, 2021, 4:29am UTC](https://discourse.laminas.dev/t/populate-select-element-in-form/2064/3 "2021-01-24T04:29:53Z")

</div>

Thank you @froschdesign . Thank you so much !!!

---

<div class="post-metadata">

**Author:** ![ebuddy](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ebuddy/32/1536_2.png) [@ebuddy](https://discourse.laminas.dev/u/ebuddy)\
**Post date:** [February 4, 2021, 7:21am UTC](https://discourse.laminas.dev/t/populate-select-element-in-form/2064/4 "2021-02-04T07:21:02Z")

</div>

Hello @froschdesign , I tried what you have said and it works. But in my controller, I get this error when I call myform-\>isvalid(). Here is the error message

**File:**

**C:\wamp\www\school-online\vendor\laminas\laminas-validator\src\AbstractValidator.php:395**

**Message:**

**Argument 1 passed to Laminas\Validator\AbstractValidator::setTranslator() must be an instance of Laminas\Validator\Translator\TranslatorInterface or null, instance of Laminas\I18n\Translator\Translator given, called in C:\wamp\www\school-online\vendor\laminas\laminas-validator\src\ValidatorPluginManager.php on line 580**

---

<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:** [February 4, 2021, 7:25am UTC](https://discourse.laminas.dev/t/populate-select-element-in-form/2064/5 "2021-02-04T07:25:17Z")

</div>

Please create a separate post for this topic because this post is marked as solved. Additionally, the new topic is then easier for others to find. Thanks in advance! 👍
