To Create a text input box I used folling code in zend framework2
use Zend\Form\Form;
class Loginform extends Form
{
public function __construct()
{
$this->add(array(
'name' => 'usernames',
'attributes' => array(
'id' => 'usernames',
'type' => 'text',
),
'options' => array(
'label' => 'User Name',
),
));
}
}
and I can populate the values in controller action using
$form = new Loginform();
$form->get('usernames')->setAttribute('value', 'user 1');
Any idea how can I do the same for Selection/drop down box in zf2?
Check the API (the docs are terrible, so check the code).
Use the
Zend\Form\Element\Selectclass and set the options attribute like so:Output the element using the
FormRoworFormSelectview helper.This site is also a good source for examples and information: http://zf2.readthedocs.org/en/latest/modules/zend.form.quick-start.html
Example:
You can also assign the options in the controller if you need to, as shown above.