Php Zend Framework 2:将选项设置为;精选;在选择列表中

Php Zend Framework 2:将选项设置为;精选;在选择列表中,php,zend-framework2,Php,Zend Framework2,我试图将第二个产品设置为列表中的选定产品,但下面的代码不起作用。任何想法。谢谢 $this->add(array( 'type' => 'Zend\Form\Element\Select', 'name' => 'manufacturer', 'options' => array( 'label' => 'Manufacturer name', 'value_option

我试图将第二个产品设置为列表中的选定产品,但下面的代码不起作用。任何想法。谢谢

$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,
        ),
    ));

如果我没有弄错的话,该值是选项的一部分,而不是属性

$this->add(array(
    'type' => 'Zend\Form\Element\Select',
    'name' => 'manufacturer',
    'options' => array(
        'label' => 'Manufacturer name',
        'value_options' => $this->getManufacturer(),
        'empty_option'  => '--- select manufacturer ---',
        'value' => '2'
    )
));

我在这里举一个简单的例子,希望能对你有所帮助。 如前所述,要获取选定元素,“简单使用值”属性具有该选定元素的值,如下所示:

    $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'
        )
    ));
如果你得到

干草堆选项是强制性的

然后将禁用\u inarray\u验证程序添加到选项:

    $this->add(array(
    ...
    'options' => array(
      'disable_inarray_validator' => true,
      'label' => 'county',
    ),
 ));

代码似乎没有问题。确保通过
$this->getManufacturer()
填充的数组中至少有一个键为
2
。还可以通过设置
'value\u options'=>array('1'=>'111','2'=>'222'),
进行测试,并检查是否选择了
222
。您是否从文档中的某个地方获取此代码<代码>元素没有“selected”属性,通常可以通过预先选择值。表单,而不是元素。
是“属性”的一部分,而不是“选项”。