Zend 框架:使用数组表示法中的表单元素 [英] Zend Framework: Working with Form elements in array notation
问题描述
我希望能够使用数组表示法在我的表单中添加一个隐藏的表单字段.我可以像这样用 HTML 做到这一点:
I would like to be able to add a hidden form field using array notation to my form. I can do this with HTML like this:
<input type="hidden" name="contacts[]" value="123" />
<input type="hidden" name="contacts[]" value="456" />
提交表单时,$_POST
数组将包含分组为数组的隐藏元素值:
When the form gets submitted, the $_POST
array will contain the hidden element values grouped as an array:
array(
'contacts' => array(
0 => '123'
1 => '456'
)
)
我可以在表单中添加一个隐藏元素,并像这样指定数组表示法:
I can add a hidden element to my form, and specify array notation like this:
$form->addElement('hidden', 'contacts', array('isArray' => true));
现在,如果我用数组填充该元素,我希望它应该将值存储为数组,并将元素呈现为如上所示的 HTML:
Now if I populate that element with an array, I expect that it should store the values as an array, and render the elements as the HTML shown above:
$form->populate($_POST);
然而,这不起作用.我正在使用的 Zend Framework 版本中可能存在错误.我这样做对吗?我应该怎么做?我怎样才能达到上面的结果?如果需要,我愿意创建自定义表单元素.让我知道我需要做什么.
However, this does not work. There may be a bug in the version of Zend Framework that I am using. Am I doing this right? What should I do differently? How can I achieve the outcome above? I am willing to create a custom form element if I have to. Just let me know what I need to do.
推荐答案
要使用数组表示法,您需要指定元素属于"父数组:
To use array notation, you need to specify that the element "belongs to" a parent array:
$form->addElement('hidden', 'contact123', array('belongsTo' => 'contacts', 'value' => '123'));
$form->addElement('hidden', 'contact456', array('belongsTo' => 'contacts', 'value' => '456'));
这篇关于Zend 框架:使用数组表示法中的表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!