Search code examples
zend-framework2

Set the dynamic values in drop down in ZF2


I am using Zend Framework 2 and I would like to understand how I can add values to a drop down that is coming from a database.

The code is here:

$this->add(
    array(
        'name'   => 'role',
        'type'   => 'Zend\Form\Element\Select', 
        'option' => array(
            'label'        => 'Role',
            'value_option' => $roleData
        )
    )
);

Here the $roleData as an array and the value of array is as follows:

Array ( [0] => Array ( [id] => 1 [cell] => admin ) [1] => Array ( [id] => 2 [cell] => member ) [2] => Array ( [id] => 3 [cell] => guest ) [3] => Array ( [id] => 4 [cell] => Admina1 ) [4] => Array ( [id] => 5 [cell] => Admina1 ) ) 

Solution

  • Try 'value_options' instead of 'value_option'.

    And your array has to be the following structure:

    $options = array(
        '1' => 'admin',
        '2' => 'member',
        '3' => 'guest',
        '4' => 'Admina1',
        '5' => 'Admina1'
    );