简体   繁体   English

如何在Zend Framework 2表单的第一个<option>上将disabled属性设置为disabled

[英]How to set the disabled attribute as disabled on the first <option> of a Zend Framework 2 form

I´m developing an application using Zend Framework 2 and I´m using two select multiple boxes. 我正在使用Zend Framework 2开发一个应用程序,我正在使用两个选择多个框。 The first is populated with data from the data base, the second is empty. 第一个是来自数据库的数据,第二个是空的。

I intend to set the disabled attribute as "disabled" in the first option of both selects. 我打算在两个选择的第一个选项中将disabled属性设置为“disabled”。 This way the first options will be unusable and un-clickable. 这样,第一个选项将无法使用且无法点击。 Therefor a user will not be able to pass those first options from one select to the other while using the add/remove buttons. 因此,当使用添加/删除按钮时,用户将无法将这些第一选项从一个选项传递到另一个选项。

Select 1 选择1

   <select id="AttributesId" multiple="multiple" size="7" title="Select an Attribute" name="idAttributes[]">
   <option value="0">Please Select an Attribute</option>
    <option value="1"> Attribute 1</option>
    <option value="2"> Attribute 2</option>
   </select>

Select 2 选择2

<select id="SelectedAttributesId" multiple="multiple" size="7" title="Selected Attributes" name="selectedAttributes[]">
<option value="0">Current Selection</option>
</select>

The php code that generates both selects on ZF2 is: 在ZF2上生成两个选择的php代码是:

(...) (......)

public function __construct ($em = null)
    {
        parent::__construct("typeset");
        $this->setAttribute("method", "post")
            ->setAttribute("class", "contact-form");

        if(null !== $em)
        {
            $this->setEntityManager($em);
        }

        $em = $this->getEntityManager();
        $query = $em->createQuery("SELECT a.idAttribute, a.internalName FROM ProductCatalog\Entity\Attribute\Attribute a ORDER BY a.internalName ASC");
        $attributes = $query->getResult();

        $select = new Element\Select('idAttributes');
        $select->setAttribute('title', 'Select an Attribute')
                ->setAttribute('size', 7)
                ->setAttribute('multiple', 'multiple')
                ->setAttribute('id', 'AttributesId');

        $selected = new Element\Select('selectedAttributes');
        $selected->setAttribute('title', 'Selected Attributes')
               ->setAttribute('size', 7)
               ->setAttribute('multiple', 'multiple')
               ->setAttribute('id', 'SelectedAttributesId');

        $labelIdAttributes = 'Attributes List: ';
        $labelSelectedAttributes = 'Selected Attributes List: ';

        $options[0] = 'Please Select an Attribute';

// this followin line doesn´t work but you can get an idea of what I need with it //$options[0]->setAttribute('deselect', 'deselect'); //这条跟随线不起作用,但你可以用它来了解我需要的东西// $ options [0] - > setAttribute('deselect','deselect');

        foreach ($attributes as $key => $value)
        {
            $options[$value['idAttribute']] = $value['internalName'];
        }

        $selectedOptions[0] = 'Current Selection';



        $select->setLabel($labelIdAttributes)
               ->setValueOptions($options);


        $selected->setLabel($labelSelectedAttributes)
                ->setValueOptions($selectedOptions);


        $this->add($select);
        $this->add($selected);

(...)

If I remember correctly you need to format the options array slightly differently: 如果我没记错的话,你需要稍微不同地格式化选项数组:

$options = array(
    array('value' => '0', 'label' => ' Please Select an Attribute', 'disabled' => 'disabled'),
    array('value' => '1', 'label' => ' Attribute 1'),
    array('value' => '2', 'label' => ' Attribute 2')
);

Disabled is one of the valid attributes. 禁用是有效属性之一。

只需获取表单元素并使用disabled属性即可

$form->get('select_element')->setAttribute('disabled', 'disabled');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM