Zend2 Form - set attributes to options in select element

I wonder how to add some attributes to select's options in Zend 2. My form element looks like this:

        'type' => 'Zend\Form\Element\Select',
        'name' => 'brand_name',
        'options' => array(
            'label' => 'Choose brand',
            'value_options' => $this->getBrands(),

So it's populate select looking similar to this:

<select name="brand_name"><option value="1">aaaa</option></select>

But I want to achieve something like this:

<select name="brand_name"><option value="1" class="test">aaaa</option></select>

And the classes should be different, depends on particular option. Is it possible? Why am I trying this? I need to add jquery plugin - chained which needs classes for options.


I've found a solution, array of parent select value_options should look like this:

foreach ($data as $item) {
        $models[] = array(
          'label' => $item->model_name, 
          'attributes' => array('class' => $item->id_brand_list), 
          'value' => $item->id);

If your data were array that you preferred getting from sql or db, you can code like:

public function getOptionsForDataSelect()
    $dbAdapter = $this->_adapter;
    $sql = "SELECT * FROM db_name ORDER BY db_name ASC";
    $statement = $dbAdapter->query($sql);
    $result = $statement->execute();

    $selectData = array();
    foreach ($result as $res) {
        $selectData[] = array(
            'label' => $res['label_data'],
            'attributes' => array(
                'data-key' => $res['data_id']
            'value' => $res['value_id']
        // $selectData[$res['id']] = $res['name'];
    return $selectData;

Need Your Help

Android App Progress Bar Won't Update When Using Thread and AsyncTask

java android multithreading android-asynctask progress-bar

I have an android app with a class, WifiAudioActivity that extends the Activity class.

"skipping: no hosts matched" issue with Vagrant and Ansible

debian vagrant virtualbox ansible

I have installed Vagrant, VirtualBox and Ansible and trying to run provision over one host but it always returns "skipping: no hosts matched"