Zend Framework 2: how to set options in select from view

In this tutorial I will explain how to set options in select from view in Zend Framework 2.5.

So, in my model form I will add this code:

$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'category',
'attributes' => array(
'class'=>'form-control'
),
));

Then, In my view I will display the select box:


echo $this->formSelect($form->get('category'));

Now, I will add new options in my select box:

I will replace


echo $this->formSelect($form->get('category'));

to

$category = $form->get('category');
$category->setValueOptions(array('0' => 'Select Category','1' => 'First Category'));
echo $this->formSelect($category);

Video tutorial:









Leave a Comment