Hello everyone.
I have created custom select element populated by table entries.
I would like to know that how can I use a where clause in my element factory to avoid creating multiple custom select element populated from the same table with different where clauses?
For instance, my first select element get all the values from the table and the second get values with a where condition.
How can I do it without creating two separate select elements?
How can I set my where clause in my factory and use it properly?
OK, this is coming from the Mezzio world, so maybe doesn’t apply to MVC, but I think it does…
If your select element’s options depend on stuff from the request - like a customer ID passed in the URL used in a query - it should be the job of the handler / controller to populate it. Pushing database stuff down to the form level is a perversion encouraged in some old tutorials. Keep them separated.
$id = $request->getAttribute('id');
$form = $this->form;
$form->get('mySelect')->setValueOptions($this->getMySelectOptions($id));
Forms are horrible - they mix presentation concerns (the fundamental fieldset → element structure, along with labels and attributes), with validation and all the rest. Don’t make it worse by adding a database to the mix.
Thank for your help. But I do not think this solution suitable. According to your example, I do not need to populate my select Element when creating it.
I just need to create the Select with empty values and then populate it in my controller.
I would like to keep my controllers as simple as possible.
Take a look at the build
method of the laminas-servicemanager.