Zend_Form 使用子表单 getValues() 问题

问题描述 投票:0回答:4

我正在 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() 仅返回第二个子表单中的元素,而不是整个表单。

php zend-framework zend-form-sub-form
4个回答
2
投票

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


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);
  }
}

0
投票

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


0
投票

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

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

有时 getValues 返回的值与 $post 返回的值不同。 一定是 getValues() 错误。

© www.soinside.com 2019 - 2024. All rights reserved.