更新时间:2023-12-06 16:19:10
添加子窗体的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