Zend_Form using subforms getValues() problem

独自空忆成欢 提交于 2019-12-11 05:36:57

问题


I am building a form in Zend Framework 1.9 using subforms as well as Zend_JQuery being enabled on those forms. The form itself is fine and all the error checking etc is working as normal. But the issue I am having is that when I'm trying to retrieve the values in my controller, I'm receiving just the form entry for the last subform e.g.

My master form class (abbreviated for speed):

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

}

So that all works as it should in the display and when I submit without filling in the required values, the correct errors are returned. However, in my controller I have this:

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

      }
    }
  }
}

When I submit this and it gets past isValid(), the $form->getValues() is only returning the elements from the second subform, not the entire form.


回答1:


I recently ran into this problem. It seems to me that getValues is using array_merge, instead of array_merge_recursive, which does render to correct results. I submitted a bug report, but have not gotten any feedback on it yet. I submitted a bug report (http://framework.zend.com/issues/browse/ZF-8078). Perhaps you want to vote on it?




回答2:


I think that perhaps I must have been misunderstanding the way that the subforms work in Zend, and the code below helps me achieve what I wanted. None of my elements share names across subforms, but I guess this is why Zend_Form works this way.

In my controller I now have:

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



回答3:


I have a same problem to get value from subforms I solve it with this but not my desire one code: in controller i get value with this code that 'rolesSubform' is my subform name $this->_request->getParam ( 'rolesSubform' );




回答4:


Encountered the same problem. Used post instead of getValues.

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

There are times when getValues does not return the same values returned by $post. Must be a getValues() bug.



来源:https://stackoverflow.com/questions/1486875/zend-form-using-subforms-getvalues-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!