【问题标题】:ZF2 Add custom attribute to option in a select form elementZF2 将自定义属性添加到选择表单元素中的选项
【发布时间】:2014-11-21 07:17:31
【问题描述】:

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

这是我的 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 中实现这一点?

谢谢。

【问题讨论】:

  • 是的,你可以在 valueOptions 中传递属性键中的键/值

标签: php forms zend-framework2


【解决方案1】:

是的,这可以通过 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,
    );

【讨论】:

  • 如果这个功能被记录下来岂不是更好!我很想编辑文档并进行 PR。
【解决方案2】:

我只是想通了这一点并想在这里分享,因为我在搜索相同的问题时看到了这个问题。应该以建议的方式给出相同的结果,但直接使用表单类中的选项属性;如果将数据对象传递给表单构造以填充像我这样的选项,则特别有用。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2020-06-12
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    相关资源
    最近更新 更多