php – 如何在Zend Framework 2表单的第一个上将disabled属性设置为disabled
作者:互联网
我正在使用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')
);
禁用是有效属性之一.
标签:php,zend-framework2,zend-form 来源: https://codeday.me/bug/20190901/1785008.html