我读过这篇文章: http://www.jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/
这非常有趣,而且效果很好。
我需要做同样的事情,但是使用子表单。我的意思是,当用户按下按钮时,我通过 ajax 调用一个操作,将子表单添加、附加并显示到我现有的表单中。
例如:
我有一个表单,用户必须填写其 child 的姓名,因此有一个“添加 child ”按钮。当用户按下该按钮时,应将子表单添加到我现有的表单中并显示。提交后,它将像该文章中的示例一样进行验证。唯一的区别是他只在其中添加了一个字段。我需要添加一个子表单,但方式完全相同。
我在我的操作中尝试了以下操作(由 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();
}
这几乎有效。
此操作的 View 返回子表单的 html 代码,因此在我的 ajax 调用成功后,我只显示它。
问题是,在提交时它会验证表单,但它丢失了刚刚添加的新子表单。在只有一个元素的文章中不会发生这种情况。我想我只需要将子表单添加到现有表单中,但是如何添加?
最佳答案
向子表单元素添加子表单的前缀。我使用前缀“child”来表示子表单。每个子表单将被创建为 child1、child2 等等。
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 函数中,使用子表单前缀而不是字段名称来过滤子表单:
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);
}
}
添加新表单功能创建每个子表单并附加到主表单:
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 函数。编辑代码以利用此功能。
这是另一个使用belongsTo的解决方案,为子表单元素提供数组表示法:http://www.stephenrhoades.com/?p=364
关于ajax - 提交时使用 ajax 将子表单添加到表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8069046/