簡體   English   中英

ZF2在select表單元素中為選項添加自定義屬性

[英]ZF2 Add custom attribute to option in a select form element

我想在Zend Framework 2表單中為select選項添加自定義HTML屬性。

這是我的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中實現這一目標?

謝謝。

是的,這可以通過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屬性,您不能放置任何隨機鍵/值對。 例如data- *就像以下一樣好:

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

我剛剛想到這一點,並希望在這里分享,因為我在尋找同一個問題時看到了這個問題。 應該使用建議的方式給出相同的結果,但直接在表單類中使用選項的屬性; 如果將數據對象傳遞給表單構造以填充像我這樣的選項,則特別有用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM