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

标签 php zend-form zend-framework2

我正在使用 Zend Framework 2 开发一个应用程序,并且我正在使用两个多选框。第一个填充了数据库中的数据,第二个为空。

我打算在两个选择的第一个选项中将 disabled 属性设置为“disabled”。 这样,第一个选项将无法使用且不可点击。因此,用户在使用添加/删除按钮时将无法将这些第一个选项从一个选择传递到另一个选择。

选择 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>

选择2

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

在 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';

//这条后续行不起作用,但你可以了解我需要什么 //$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);

(...)

最佳答案

如果我没记错的话,您需要对选项数组进行稍微不同的格式化:

$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 是有效属性之一。

关于php - 如何在 Zend Framework 2 表单的第一个 <option> 上将禁用属性设置为禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12915689/

相关文章:

php - zf2 插入使用 $db->insert($table, $data);风格

php - 如何通过 PHP 和 mysql 正确转义字符串

scripting - Zend Form - 从表单类中提取标签值以在 View 脚本中显示的方法?

php - 如何删除 Zend Framework 中的表单属性?

zend-framework2 - 如何在 Zend2 的删除查询中使用 limit 和 order by

php - ZF2 中正确的 JSON 输出,无需模板渲染

javascript - 如何在进行ajax调用时不在控制台下显示整个url

php - 用数字表示文本 MySQL

php - ZEND 和 MySQL - 如何搜索和 ORDER BY(排序)相关性?

php - 提供给 Escape 助手的对象,但标志不允许在 Zend Framework 2 中递归