Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 Add custom attribute to option in a select form element

I would like to add a custom HTML attribute to an option of a select in a Zend Framework 2 Form.

This is my (partial) code from my Form class:

$this->add(array(
        'name' => 'lieuRemplissage',
        'type' => 'Select',
        'attributes'    => array(
            'class'     => 'form-control',
        ),
        'options'   => array(
            'label' => _('Lieu pré-enregistré'),
        ),
    ));

I populate my options values in my controller like this :

$form = new \Vente\Form\Vente;
foreach($this->getAdminLieuDeVenteTable()->fetchAll() as $lieu) {
       $optionsLieu[$lieu->getId()] = $lieu->getNom();
    }
    $form->get('lieuRemplissage')->setValueOptions($optionsLieu);

But now, for each option I want to add an html attribute to all select options but with a different value for each one.

Is there a way do achieve that in ZF2 ?

Thanks.

like image 937
user3260788 Avatar asked Sep 26 '14 09:09

user3260788


2 Answers

Yes this is possible with ZF2

You pass in the attributes within the option value. The value should be in array format:

//example in view:

$select=new \Zend\Form\Element\Select('test');
$select->setValueOptions(

    [

        ['attributes'=>['data-key'=>'value'],'value'=>'myValue','label'=>'myLabel']


    ]

    );

echo $this->formselect($select);

prints:

<select name="test"><option value="myValue" data-key="value">myLabel</option></select>

EDIT:

The attributes you provide must be valid HTML attributes you cannot put any random key/value pairs. For example data-* is fine as are the following :

protected $validGlobalAttributes = array(
        'accesskey'          => true,
        'class'              => true,
        'contenteditable'    => true,
        'contextmenu'        => true,
        'dir'                => true,
        'draggable'          => true,
        'dropzone'           => true,
        'hidden'             => true,
        'id'                 => true,
        'lang'               => true,
        'onabort'            => true,
        'onblur'             => true,
        'oncanplay'          => true,
        'oncanplaythrough'   => true,
        'onchange'           => true,
        'onclick'            => true,
        'oncontextmenu'      => true,
        'ondblclick'         => true,
        'ondrag'             => true,
        'ondragend'          => true,
        'ondragenter'        => true,
        'ondragleave'        => true,
        'ondragover'         => true,
        'ondragstart'        => true,
        'ondrop'             => true,
        'ondurationchange'   => true,
        'onemptied'          => true,
        'onended'            => true,
        'onerror'            => true,
        'onfocus'            => true,
        'oninput'            => true,
        'oninvalid'          => true,
        'onkeydown'          => true,
        'onkeypress'         => true,
        'onkeyup'            => true,
        'onload'             => true,
        'onloadeddata'       => true,
        'onloadedmetadata'   => true,
        'onloadstart'        => true,
        'onmousedown'        => true,
        'onmousemove'        => true,
        'onmouseout'         => true,
        'onmouseover'        => true,
        'onmouseup'          => true,
        'onmousewheel'       => true,
        'onpause'            => true,
        'onplay'             => true,
        'onplaying'          => true,
        'onprogress'         => true,
        'onratechange'       => true,
        'onreadystatechange' => true,
        'onreset'            => true,
        'onscroll'           => true,
        'onseeked'           => true,
        'onseeking'          => true,
        'onselect'           => true,
        'onshow'             => true,
        'onstalled'          => true,
        'onsubmit'           => true,
        'onsuspend'          => true,
        'ontimeupdate'       => true,
        'onvolumechange'     => true,
        'onwaiting'          => true,
        'role'               => true,
        'aria-labelled-by'   => true,
        'aria-described-by'  => true,
        'spellcheck'         => true,
        'style'              => true,
        'tabindex'           => true,
        'title'              => true,
        'xml:base'           => true,
        'xml:lang'           => true,
        'xml:space'          => true,
    );
like image 119
matwr Avatar answered Oct 29 '22 06:10

matwr


I just figured this out and wanted to share here since I saw this question while I was searching for the same question. Should give the same result with the suggested way but directly using options' attributes in form class; especially useful if passing data object to form construct to populate options like me.

$this->add(array(
    'name' => 'lieuRemplissage',
    'type' => 'Select',
    'attributes'    => array(
        'class'     => 'form-control',
    ),
    'options'   => array(
        'label' => _('Lieu pré-enregistré'),
        'value' => 123
        'attributes' => array(
            'data-key' => 'value_for_data_attribute_goes_here',
        ),
    ),
));
like image 45
smozgur Avatar answered Oct 29 '22 07:10

smozgur