从 Zend_Form_SubForms 检索值时出现问题 - 没有返回值

发布于 2024-08-17 06:30:50 字数 893 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极度宠爱 2024-08-24 06:30:50

这就是我所做的 -

$bigForm->addElements($littleForm1->getElements());

然后,像这样迭代表单元素:

    $displayGroup1 = array();

    foreach($bigForm->getElements() as $name=>$value){

        array_push($displayGroup1, $name);
    }

然后,将 displayGroup 添加到 $bigForm:

   $bigForm->addDisplayGroup($displayGroup1, 'test',array('legend'=>'Test'));

并对多个显示组重复此操作。

我确信有一种更好的方法可以做到这一点,但我目前无法找到一种方法。
如果表单由一个或多个子表单组成,这是目前我能想到的通过 $_POST 检索所有表单值的一种方法。
如果有人知道更好的解决方案,请发布!

This is what I did -

$bigForm->addElements($littleForm1->getElements());

Then, iterated over the form elements like so:

    $displayGroup1 = array();

    foreach($bigForm->getElements() as $name=>$value){

        array_push($displayGroup1, $name);
    }

Then, add a displayGroup to $bigForm:

   $bigForm->addDisplayGroup($displayGroup1, 'test',array('legend'=>'Test'));

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!

葮薆情 2024-08-24 06:30:50

Zend_Form 不会自动从 $_POST 变量中检索值。使用:

$bigform->populate($_POST)

或者:

$bigform->populate($this->_request->getPost())

另一件要记住的事情是,如果子表单包含具有相同名称的元素,它们将会发生冲突。要检查这一点,请使用选项 View =>在浏览器中查看页面源并查看生成的 HTML。当您看到两个 元素具有相同的 name 属性时,这就是问题所在。

Zend 的解决方案是使用 setElementsBelongTo 为子表单元素指定不同的名称:

$littleForm1->setElementsBelongTo('littleForm1');
$littleForm2->setElementsBelongTo('littleForm2');

此外,您应该忽略这些调用,因为它们没有任何作用(但您应该为 $bigForm< /代码>):

$littleForm->setMethod('post');

A Zend_Form does not automatically retrieve values from the $_POST variable. Use:

$bigform->populate($_POST)

Or alternatively:

$bigform->populate($this->_request->getPost())

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 same name attribute, then this is the problem.

The Zend solution for this is to give the sub form elements different names using setElementsBelongTo:

$littleForm1->setElementsBelongTo('littleForm1');
$littleForm2->setElementsBelongTo('littleForm2');

Furthermore you should leave out these calls as they serve no purpose (but you should set them for the $bigForm):

$littleForm->setMethod('post');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文