In ZF2, suppose I have a Select
in the form:
$this->add([
'type' => 'Zend\Form\Element\Select',
'name' => 'someName',
'attributes' => [
'id' => 'some-id',
],
'options' => [
'label' => 'Some Label',
'value_options' => [
'1' => 'type 1',
'2' => 'type 2',
'3' => 'type 3',
],
],
]);
How can I put the values 'type 1', 'type 2', 'type 3', etc. from a database query into value_options
?
By registering a custom select element with the form element manager, you can use a factory to load the required form options.
And the required configuration for
module.config.php
.You can then use
MyModule\\Form\\Element\\TypeSelect
as thetype
value when adding the element to a form.Also make sure to read the documentation regarding custom form elements; this describes how to use the form element manager correctly, essential for the above to work.