从 Zend_Form_SubForms 检索值时出现问题 - 没有返回值
我有一个 Zend_Form 有 4 个或更多子表单。
/** 代码片段 **/ $bigForm = new Zend_Form();
$littleForm1 = new Form_LittleForm1();
$littleForm1->setMethod('post');
$littleForm2 = new Form_LittleForm2();
$littleForm2->setMethod('post');
$bigForm->addSubForm($littleForm1,'littleForm1',0);
$bigForm->addSubForm($littleForm2,'littleForm2',0);
单击“提交”按钮后,我尝试打印出输入表单的值,如下所示:
/** 代码片段,当前未验证,仅打印 **/
if($this->_request->getPost()){ $formData = 数组();
foreach($bigForm->getSubForms() as $subForm){
$formData = array_merge($formData, $subForm->getValues());
}
/* Testing */
echo "<pre>";
print_r($formData);
echo "</pre>";
最终
结果是 - 表单中的所有元素都会被打印,但在发布表单之前输入的值不会被打印。
任何想法都值得赞赏......我一直在努力解决这个问题!
提前致谢!
I have a Zend_Form that has 4 or more subforms.
/**
Code Snippet
**/
$bigForm = new Zend_Form();
$littleForm1 = new Form_LittleForm1();
$littleForm1->setMethod('post');
$littleForm2 = new Form_LittleForm2();
$littleForm2->setMethod('post');
$bigForm->addSubForm($littleForm1,'littleForm1',0);
$bigForm->addSubForm($littleForm2,'littleForm2',0);
On clicking the 'submit' button, I'm trying to print out the values entered into the forms, like so:
/**
Code snippet, currently not validating, just printing
**/
if($this->_request->getPost()){
$formData = array();
foreach($bigForm->getSubForms() as $subForm){
$formData = array_merge($formData, $subForm->getValues());
}
/* Testing */
echo "<pre>";
print_r($formData);
echo "</pre>";
}
The end result is that - all the elements in the form do get printed, but the values entered before posting the form don't get printed.
Any thoughts are appreciated...I have run around circles working on this!
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我所做的 -
$bigForm->addElements($littleForm1->getElements());
然后,像这样迭代表单元素:
然后,将 displayGroup 添加到 $bigForm:
并对多个显示组重复此操作。
我确信有一种更好的方法可以做到这一点,但我目前无法找到一种方法。
如果表单由一个或多个子表单组成,这是目前我能想到的通过 $_POST 检索所有表单值的一种方法。
如果有人知道更好的解决方案,请发布!
This is what I did -
$bigForm->addElements($littleForm1->getElements());
Then, iterated over the form elements like so:
Then, add a displayGroup to $bigForm:
And repeat for multiple display groups.
I'm sure there is a better way to do it, but I'm currently unable to find one out.
This is currently one way I can think of to retrieve all the form values via $_POST, if a form is made up of one or more subforms.
If any one knows of a better solution, please post it!
Zend_Form
不会自动从$_POST
变量中检索值。使用:或者:
另一件要记住的事情是,如果子表单包含具有相同名称的元素,它们将会发生冲突。要检查这一点,请使用选项 View =>在浏览器中查看页面源并查看生成的 HTML。当您看到两个
元素具有相同的
name
属性时,这就是问题所在。Zend 的解决方案是使用 setElementsBelongTo 为子表单元素指定不同的名称:
此外,您应该忽略这些调用,因为它们没有任何作用(但您应该为
$bigForm< /代码>):
A
Zend_Form
does not automatically retrieve values from the$_POST
variable. Use:Or alternatively:
Another thing to keep in mind is that if the sub forms contain elements with the same name they will clash. To check this use the option View => Page Source in your browser and look at the HTML that is generated. When you see two
<input>
elements with the samename
attribute, then this is the problem.The Zend solution for this is to give the sub form elements different names using
setElementsBelongTo
:Furthermore you should leave out these calls as they serve no purpose (but you should set them for the
$bigForm
):