gpt4 book ai didi

php - ZF2 将自定义属性添加到选择表单元素中的选项

转载 作者:可可西里 更新时间:2023-11-01 12:29:05 26 4
gpt4 key购买 nike

我想将自定义 HTML 属性添加到 Zend Framework 2 Form 中的选择选项。

这是我的 Form 类中的(部分)代码:

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

我像这样在我的 Controller 中填充我的选项值:

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

但是现在,对于每个选项,我想为所有选择选项添加一个 html 属性,但每个选项具有不同的值。

有没有办法在 ZF2 中做到这一点?

谢谢。

最佳答案

是的,这在 ZF2 中是可能的

您在选项值中传递属性。该值应为数组格式:

// View 示例:

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

关于php - ZF2 将自定义属性添加到选择表单元素中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26056832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com