2014-09-26 9 views
8

Chciałbym dodać niestandardowy atrybut HTML do opcji wyboru w formularzu Zend Framework 2.ZF2 Dodaj atrybut niestandardowy do opcji w formularzu wyboru elementu

To jest mój (częściowe) kod z mojej klasy Kształt:

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

I wypełnić moje opcje wartości w moim kontroler następująco:

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

Ale teraz, dla każdego wariantu Chcę dodaj atrybut html do wszystkich wybranych opcji, ale z inną wartością dla każdego z nich.

Czy można to osiągnąć w ZF2?

Dzięki.

+0

tak nie jest, można może przekazać klucz/wartość w kluczu atrybutów wewnątrz valueOptions – matwr

Odpowiedz

5

Właśnie zorientowałem się, i chciał podzielić się tutaj, ponieważ widziałem to pytanie podczas Szukałem tym samym pytaniem. Powinien dać taki sam wynik w sugerowany sposób, ale bezpośrednio przy użyciu atrybutów opcji w klasie formularza; szczególnie użyteczne, jeśli przekazujemy obiekt danych, aby utworzyć konstrukt, aby zapełnić takie opcje jak ja.

$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', 
     ), 
    ), 
)); 
10

Tak jest to możliwe z ZF2

przekazać w atrybutach w wartości opcji. Wartość ta powinna być w formacie tablicy:

// przykład w widoku:

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

    [ 

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


    ] 

    ); 

echo $this->formselect($select); 

drukuje:

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

EDIT:

atrybutów podasz musi być ważny html atrybuty nie można umieścić losowych par klucz/wartość. Na przykład DATA- * jest w porządku, są następujące:

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, 
    ); 
+0

czy nie byłoby miło, gdyby ta funkcja zostały udokumentowane! Mam ochotę edytować dokumenty i zrobić PR. – David

Powiązane problemy