zend-framework2 - zf2 创建选择/下拉框并在 Controller 中填充选项?

标签 zend-framework2

要创建一个文本输入框,我在 zend framework2 中使用了以下代码

use Zend\Form\Form;

class Loginform extends Form
{
    public function __construct()
    {
           $this->add(array(            
            'name' => 'usernames',
            'attributes' =>  array(
                'id' => 'usernames',                
                'type' => 'text',
            ),
            'options' => array(
                'label' => 'User Name',
            ),
        ));       
    }
}

我可以使用填充 Controller 操作中的值
$form = new Loginform();
$form->get('usernames')->setAttribute('value', 'user 1');

知道如何对 zf2 中的选择/下拉框执行相同操作吗?

引用:zend framework 2 documentation

最佳答案

检查 API(文档很糟糕,所以检查代码)。

使用 Zend\Form\Element\Select类并像这样设置选项属性:

$element->setAttribute('options', array(
    'key' => 'val',
    ...
));

使用 FormRow 输出元素或 FormSelect查看助手。

该站点也是示例和信息的良好来源:http://zf2.readthedocs.org/en/latest/modules/zend.form.quick-start.html

例子:
$this->add(array(     
    'type' => 'Zend\Form\Element\Select',       
    'name' => 'usernames',
    'attributes' =>  array(
        'id' => 'usernames',                
        'options' => array(
            'test' => 'Hi, Im a test!',
            'Foo' => 'Bar',
        ),
    ),
    'options' => array(
        'label' => 'User Name',
    ),
));    

如果需要,您还可以在 Controller 中分配选项,如上所示。

关于zend-framework2 - zf2 创建选择/下拉框并在 Controller 中填充选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12301421/

相关文章:

php - 想象一下 ZF2 : Ratio Resize

php - 如何从模型中获取 Zend\Db\Adapter 实例? (ZF2)

php - doctrine 2 和 zend framework 2 如何使用缓存?

php - 使用 BINARY 查询 ZF2 模型

zend-framework2 - 在共享托管服务器上安装 ZF2

php - ZF 2 Zend\Form\Element\Date 不在 View 中创建日历

php - 是否支持在 1 个查询中进行多次插入?

php - ZF2 - 如何在 columns() 中使用 SQL_CALC_FOUND_ROWS

php - 如何让 PHPUnit 忽略文件模式?

zend-framework2 - 禅2 : How to override module's configuration file?