1

我正在 Zend Framework 1.9 中使用子表单以及在这些表单上启用 Zend_JQuery 构建一个表单。表单本身很好,所有错误检查等都正常工作。但是我遇到的问题是,当我尝试检索控制器中的值时,我只收到最后一个子表单的表单条目,例如

我的主表单类(速度的缩写):

Master_Form extends Zend_Form
{

  public function init()
  {

    ZendX_JQuery::enableForm($this);

    $this->setAction('actioninhere')
         ...
         ->setAttrib('id', 'mainForm')

    $sub_one = new Form_One();
    $sub_one->setDecorators(... in here I add the jQuery as per the docs);
    $this->addSubForm($sub_one, 'form-one');

    $sub_two = new Form_Two();
    $sub_two->setDecorators(... in here I add the jQuery as per the docs);
    $this->addSubForm($sub_two, 'form-two');
  }

}

这样所有的东西都可以在显示中正常工作,并且当我在未填写所需值的情况下提交时,会返回正确的错误。但是,在我的控制器中,我有这个:

class My_Controller extends Zend_Controller_Action
{
  public function createAction()
  {
    $request = $this->getRequest();
    $form = new Master_Form();

    if ($request->isPost()) {
      if ($form->isValid($request->getPost()) {

        // This is where I am having the problems
        print_r($form->getValues());

      }
    }
  }
}

当我提交它并且它通过 isValid() 时,$form->getValues() 仅返回来自第二个子表单的元素,而不是整个表单。

4

4 回答 4

2

我最近遇到了这个问题。在我看来,getValues 使用的是 array_merge,而不是 array_merge_recursive,它确实呈现正确的结果。我提交了一个错误报告,但还没有得到任何反馈。我提交了一份错误报告(http://framework.zend.com/issues/browse/ZF-8078)。也许你想投票?

于 2009-10-21T17:44:51.007 回答
0

我想也许我一定是误解了 Zend 中子表单的工作方式,下面的代码帮助我实现了我想要的。我的元素没有跨子表单共享名称,但我想这就是 Zend_Form 以这种方式工作的原因。

在我的控制器中,我现在有:

if($request->isPost()) {
  if ($form->isValid($request->getPost()) {
    $all_form_details = array();
    foreach ($form->getSubForms() as $subform) {
      $all_form_details = array_merge($all_form_details, $subform->getValues());
    }
    // Now I have one nice and tidy array to pass to my model. I know this
    // could also be seen as model logic for a skinnier controller, but
    // this is just to demonstrate it working.
    print_r($all_form_details);
  }
}
于 2009-09-28T13:21:17.180 回答
0

我有一个从子表单获取价值的问题我用这个解决了它,但不是我想要的一个代码:在控制器中,我用这个代码获得价值,'rolesSubform' 是我的子表单名称 $this->_request->getParam ('rolesSubform' );

于 2010-03-28T04:59:20.170 回答
0

遇到了同样的问题。使用 post 而不是 getValues。

$post = $this->getRequest()->getPost();

有时 getValues 不会返回与 $post 返回的值相同的值。必须是 getValues() 错误。

于 2013-06-25T05:18:31.013 回答