2014-09-26 121 views
8

我想將自定義HTML屬性添加到Zend Framework 2表單中選擇的選項。ZF2將自定義屬性添加到選擇表單元素中的選項

這是我的(部分)的代碼從我的Form類:

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

我填充我的選擇值在我的控制器是這樣的:

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

但是現在,每個選項我想爲所有選項添加一個html屬性,但每個選項的值不同。

有沒有辦法做到這一點在ZF2?

謝謝。

+0

是有,你可以在屬性鍵傳遞中valueOptions – matwr 2014-10-30 16:06:27

回答

5

我想通了這一點,並希望,因爲我看到了這個問題,而我正在尋找同樣的問題在這裏分享。應該以建議的方式給出相同的結果,但直接使用選項的表單類屬性;如果傳遞數據對象以形成構造來填充像我這樣的選項,那麼特別有用。

$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

是的,這是可能的ZF2

您傳遞選項值內的屬性。該值應在陣列形式:

//例如在視圖:

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

    [ 

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


    ] 

    ); 

echo $this->formselect($select); 

打印:

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

編輯:

的屬性時,提供必須是有效的HTML屬性你不能放任何隨機的鍵/值對。 例如數據 - *是罰款,有以下幾種:

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

鍵/值豈不是很好,如果這個功能被記錄在案!我很想編輯文檔並製作公關。 – David 2017-09-21 16:56:28

相關問題