zend-form - Zend_Form数组符号和空元素名称

标签 zend-form zend-form-sub-form

我想渲染:

<input type="text" value="" name="foo[]" />
<input type="text" value="" name="bar[]" />

但是Zend_Form_Element需要一个(字符串)名称,所以我需要这样做:
$this->addElement('text', '1', array(
    'belongsTo' => 'foo'
));

$this->addElement('text', '2', array(
    'belongsTo' => 'bar'
));

但是输出是:
<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-2"  type="text" value="" name="bar[2]" />

我也可以接受类似的输出:
<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-1"  type="text" value="" name="bar[1]" />

但是Zend_Form_Element重写具有相同名称的元素

有什么方法可以做我需要的吗?

最佳答案

对于多个值:

$foo = new Zend_Form_Element_Text('foo');
// Other parameters
$foo->setIsArray(TRUE);
$this->addElement($foo);

生成:name="foo[]"
--

如果您要查找给定的 key (例如name="foo[bar]"),请使用:
$bar= new Zend_Form_Element_Text('bar');
// Other parameters
$bar->setBelongsTo('foo');
$this->addElement($bar);

--

在ZF 1.11.5上测试

关于zend-form - Zend_Form数组符号和空元素名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1638706/

相关文章:

css - 为 Zend_Form_Element_Checkbox 设置装饰器

php - Zend_Form : Using HtmlTag Decorator twice?

zend-framework - Zend_Form int 验证..?

orm - 使用 Zend Form 保存包含 ObjectSelect 元素的 Doctrine 2 实体

zend-framework - Zend Framework 动态添加表单字段并填充

php - 未找到类 'Zend_Form_Subform'

zend-framework - Zend_Form 和分页