要创建一个文本输入框,我在 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/