我正在尝试将第二个产品设置为在列表中选中,但下面的代码不起作用。任何的想法。谢谢
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'manufacturer',
'options' => array(
'label' => 'Manufacturer name',
'value_options' => $this->getManufacturer(),
'empty_option' => '--- select manufacturer ---',
),
'attributes' => array(
'value' => 2,
'selected' => true,
),
));
最佳答案
我这里举个简单的例子,希望对你有帮助。 正如您提到的获取选定元素一样,简单使用 value 属性具有该选定元素的值,例如:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'gender',
'options' => array(
'label' => 'Gender',
'value_options' => array(
'1' => 'Select your gender',
'2' => 'Female',
'3' => 'Male'
),
),
'attributes' => array(
'value' => '1' //set selected to '1'
)
));
you can prefer this link for more 如果你得到 p>
haystack option is mandatory
然后将 disable_inarray_validator 添加到选项中:
$this->add(array(
...
'options' => array(
'disable_inarray_validator' => true,
'label' => 'county',
),
));
关于php - Zend 框架 2 : Set options as "selected" in select list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26235033/