$requestSubform= new Zend_Form_Subform();
$requestSubform->setElementsBelongTo("requestRow[$rowNumber]");
// add elements to $requestSubform
// Now create the file upload subform
$fileUploadSubform= new Zend_Form_SubForm();
$fileUploadSubform->addElement('file','fileName')
->setLabel('File');
$fileUploadSubform->addElement('text','fileDesc')
->setLabel('File Description');
$requestSubform->addSubForm($fileUploadSubform,'fileUpload');
$this->view->field = $requestSubform->__toString();
// pass it as json via ajax back to javascript
当呈现表单时,$fileUploadSubform fileDesc元素的名称和id如下所示
name="requestRow[1][requestRow][1][fileUpload][fileDesc]" id="requestRow-1-fileUpload-fileDesc"
为什么我在setElementsBelongTo()函数中设置的值重复两次?
先谢谢你!
[更新08/13/2015]
作为临时解决方法,我刚从子子窗体($fileUploadSubform)而不是父子窗体($requestSubform)调用setElementsBelongTo()
[更新08/17/2015]
我已经尝试了从http://zend-framework-community.634137.n4.nabble.com/Improved-array-support-for-Zend-Form-td667215.html获得的以下代码,因为它表示子表单元素正在正常工作.
$form = new Zend_Form();
$form->setElementsBelongTo('foobar');
$form->addElement('text','firstName')
->getElement('firstName')
->setLabel('First Name')
->setrequired(true);
$form->addElement('text','lastName')
->getElement('lastName')
->setLabel('Last Name')
->setrequired(true);
$subForm = new Zend_Form_SubForm();
$subForm->setElementsBelongTo('foobar[baz]');
$subForm->addElement('text','email')
->getElement('email')
->setLabel('Email Address');
$subSubForm = new Zend_Form_SubForm();
$subSubForm->setElementsBelongTo('foobar[baz][bat]');
$subSubForm->addElement('checkBox','home')
->getElement('home')
->setLabel('Home address?');
$subForm->addSubForm($subSubForm,'subSub');
$form->addSubForm($subForm,'sub')
->addElement('submit','save',array('value' => 'submit'));
print_r($form->__toString());
但是这是我得到的$subForm和$subFubForm的元素.
<input id="foobar-foobar-baz-email" type="text" value="" name="foobar[foobar][foobar][baz][email]"> <input id="foobar-foobar-baz-foobar-baz-bat-home" type="checkBox" value="1" name="foobar[foobar][foobar][baz][foobar][foobar][baz][foobar][baz][bat][home]">
[更新08/24/2015]
我终于想出了这个问题.
这是这条线
$this->view->field = $additionalInfoSubform->__toString();
之前有一些元素没有显示,所以我补充说.只有现在我明白那些没有出现的元素是没有ViewHelper装饰器的元素.所以,当我将ViewHelper设置为装饰器,并删除上述字段并调用子表单的setElementsBelongTo(),而不必从该子表单的层次结构的根目录开始.
所以当你宣布:
$form = new Zend_Form();
$form->setElementsBelongTo('foo');
$subForm = new Zend_Form_SubForm();
$subForm->setElementsBelongTo('bar');
$subForm->addElement('text','email')
$form->addSubForm($subForm,'sub');
这被解释为将电子邮件放入酒吧和酒吧进入foo,aka:
name="foo[bar][email]"
文件说:
setElementsBelongTo (line 1367)
Set name of array elements belong to
access: public
Zend_Form setElementsBelongTo (string $array)
string $array
从http://framework.zend.com/apidoc/1.9/Zend_Form/Zend_Form.html#setElementsBelongTo
也:
Zend_Form::setElementsBelongTo($array)
Using this method,you can specify the name of an array to which all elements of the form belong. You can determine the name using the getElementsBelongTo() accessor.
从http://framework.zend.com/manual/1.12/en/zend.form.advanced.html
措辞可能有点不清楚,但可能支持我的理论.所以当使用$form-> setElementsBelongTo(‘foo’)时,添加到$form的任何东西都将成为foo的一个元素,因此,foo必须被排除在处理子元素的后续setElementsBelongTo()调用之外.