我有一个包含4个子窗体或更多子窗体的Zend_Form。
新代码片段**/ $bigForm = /** 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>";}
最终结果是-表单中的所有元素都会打印出来,但在发布表单之前输入的值不会打印出来。
任何想法都很感谢……我已经在这上面跑来跑去了!
提前感谢!
发布于 2010-01-08 02:44:13
我就是这么做的-
$bigForm->addElements($littleForm1->getElements());
$displayGroup1 = array();
foreach($bigForm->getElements() as $name=>$value){
array_push($displayGroup1, $name);
}然后,向$bigForm添加一个displayGroup:
$bigForm->addDisplayGroup($displayGroup1, 'test',array('legend'=>'Test'));并对多个显示组重复此操作。
我相信有更好的方法可以做到这一点,但我目前找不到一个。如果一个表单由一个或多个子表单组成,这是目前我能想到的一种通过$_POST检索所有表单值的方法。如果有人知道更好的解决方案,请把它贴出来!
发布于 2010-01-08 00:54:15
Zend_Form不会自动从$_POST变量中检索值。使用:
$bigform->populate($_POST)或者:
$bigform->populate($this->_request->getPost())另一件要记住的事情是,如果子窗体包含具有相同名称的元素,它们将发生冲突。要检查这一点,请在浏览器中使用查看HTML选项,并查看生成的=>。当您看到两个具有相同name属性的<input>元素时,这就是问题所在。
Zend的解决方案是使用setElementsBelongTo为子窗体元素指定不同的名称
$littleForm1->setElementsBelongTo('littleForm1');
$littleForm2->setElementsBelongTo('littleForm2');此外,您应该省略这些调用,因为它们没有任何用途(但您应该将它们设置为$bigForm):
$littleForm->setMethod('post');https://stackoverflow.com/questions/2021894
复制相似问题