添加子窗体形式与阿贾克斯提交 [英] Add a subform to a form with ajax on submit

查看:125
本文介绍了添加子窗体形式与阿贾克斯提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到这篇文章: 的http://www.jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/

这是非常有趣的,它工作正常。

That was very interesting and it works fine.

我需要做相同,但与子窗体。我的意思是,当用户presses一个按钮,我呼吁,通过AJAX,即增加了,高度和显示子窗体,以我现有形式的行动。

I need to do the same but with a SubForm. I mean that when a user presses a button, I call, via ajax, an action that adds, attaches and displays a subform to my existing form.

例如:
我有一个表格,其中用户必须填写姓名和他的孩子的姓,所以有一个按钮添加子。当用户presses该按钮子窗体应添加到我的存在形式和显示。在提交将验证完全像该文章中的例子。唯一的区别是,在那里,他只是增加了一个字段。我需要添加子窗体,但在完全相同的方式。

For example:
I have a form where a user must fill in the name and surname of his children, so there is a button "Add Child". When the user presses that button a SubForm should be added to my existing form and displayed. On submit it will validate exactly like the example in that article. The only difference is that in there he just adds a single field. I need to add a SubForm, but in exactly the same way.

我想在我的行动以下(阿贾克斯调用):

I tried the following in my action ( called by Ajax ):

public function clonerecursivegroupAction()
{
    $this->_helper->layout->disableLayout();
    $ajaxContext = $this->_helper->getHelper('AjaxContext');
    $ajaxContext->addActionContext('clonerecursivegroup', 'html')->initContext();

    $id = $this->_getParam('id', null);

    $subform1 = new Zend_Form_SubForm();

    $Element1 = $subform1->createElement('text', 'text1');
    $Element1->setLabel('text1')->setRequired(true);
    $Element2 = $subform1->createElement('text', 'text2');
    $Element2->setLabel('text2')->setRequired(false);

    $subform1->addElement($Element1);
    $subform1->addElement($Element2);

    $this->view->field = $subform1->__toString();
}

这几乎工程。
这个动作的视图返回子窗体的HTML code,所以在我的Ajax调用成功,我只是显示它。

This almost works.
The view of this action returns the html code of the SubForm, so on success of my ajax call I just display it.

的问题是,在提交验证的形式,但它已经失去了刚刚添加的新子。这不会发生在文章中只用一个元素。我想我只需要子窗体添加到现有的表,但如何?

The problem is that on submit it validates the form but it has lost the new subform just added. That does not happen in the article with just one element. I think I just need to add the SubForm to the existing Form, but how?

推荐答案

添加子窗体的preFIX到子窗体元素。我用了preFIX孩子重新present子窗体。每个子窗体将创建为child1,的child2等

Add a prefix of the subform to the subform elements. I used the prefix "child" to represent the subforms. Each subform will be created as child1, child2 and so on.

public function clonerecursivegroupAction()
{
       //.. Other code

        $subform = new Zend_Form_SubForm();
    $subform->setIsArray(true);
    $subform->setName("child$id");
    $Element1 = $subform->createElement('text', "newfield$id");
    $Element1->setLabel("newfield$id")
             ->setRequired(true);
    $subform->addElement($Element1);

    $Element1 = $subform->createElement('text', "nextfield$id");
    $Element1->setLabel("nextfield$id")
             ->setRequired(true);

    $subform->addElement($Element1);

    $this->view->field = $subform; 
 // Rest of your statements

}

然后,在preValidation功能,使用窗体preFIX筛选,而不是字段名的子表单:

Then, in the preValidation function, filter the subforms using the subform prefix, instead of the field name:

   public function preValidation(array $data) {
         // array_filter callback
        function findForms($field) {
         // return field names that include 'child'
          if (strpos($field, 'child') !== false) {
               return $field;
           }
         }

      $subForms = array_filter(array_keys($data), 'findForms'); //filter the subform elements

      $children = array();
      foreach ($subForms as $subform) {

          if (is_array($data[$subform])) { 
        $children[$subform] = $data[$subform];
      }

       }

       //Iterate the children
       foreach ($children as $key => $fields) { //$key = subformname, $field=array containing fiend names and values

       // strip the id number off of the field name and use it to set new order
       $order = ltrim($key, 'child') + 2;
       $this->addNewForm($key, $fields, $order);
     }

}

添加新的表单功能创建每个子表单和重视,主要形式有:

Add New Form function creates each of the sub forms and attaches to the main form:

     public function addNewForm($form, $elements, $order) {

            $subform = new Zend_Form_SubForm();
    $subform->setIsArray(true);
    foreach ($elements as $key => $el) {
          $Element1 = $subform->createElement('text', $key);
              $Element1->setLabel($form.$key)
             ->setValue($el)
                 ->setRequired(true);
                   $subform->addElement($Element1);
    }
        $this->addSubForm($subform, $form, $order);

    }

使用setIsArray为子窗体创建窗体为数组元素中的每个元素。它简化了preValidate功能。编辑code,利用此功能。

Using setIsArray for a subform creates each element of the subform as an array element. It simplifies the preValidate function. Edited the code to utilize this feature.

请参见完整的code在引擎收录

下面是用另一种的belongsTo解决方案,提供了数组符号到子窗体元素: HTTP:// WWW .stephenrhoades.com / P = 364

Here is another solution using belongsTo, providing array notation to the sub form elements : http://www.stephenrhoades.com/?p=364

这篇关于添加子窗体形式与阿贾克斯提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆