php - Zend 框架 2 : Set options as "selected" in select list

标签 php zend-framework2

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

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

关于php - Zend 框架 2 : Set options as "selected" in select list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26235033/

相关文章:

php - ZF2保存到多个表,(1)如果数据库错误则回滚更改(2)显示数据库错误

php - Zend 框架 : Proper way to interact with database?

php - 如果用户被禁止,则终止事件 session

javascript - 在同一页面打开 HTML 表单

zend-framework - 异常的子命名空间

php - ZF2 - ServiceManager 和 'aware' 接口(interface)

php - Zf2 Db Sessions 插入但不更新数据库中的 session

php - MySQL - 减少select语句中嵌套公式的长度

php - 如何用bb代码改变背景颜色

php - 我应该用什么来格式化地址国家格式的邮政地址