PHP-Zend Framework 2:在选择列表中将选项设置为“ selected”
作者:互联网
我正在尝试将第二个产品设置为列表中选择的产品,但是下面的代码不起作用.任何想法.谢谢
$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
如果你得到
haystack option is mandatory
然后将disable_inarray_validator添加到选项中:
$this->add(array(
...
'options' => array(
'disable_inarray_validator' => true,
'label' => 'county',
),
));
标签:zend-framework2,php 来源: https://codeday.me/bug/20191029/1957235.html