I have a subform($fileUploadSubform) within a subform ($requestSubform). I called setElementsBelongTo("requestRow[$rowNumber]") on the parent subform ($requestSubform).
$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
When the form is rendered, $fileUploadSubform fileDesc element' name and id are as follows
name="requestRow[1][requestRow][1][fileUpload][fileDesc]"
id="requestRow-1-fileUpload-fileDesc"
Why is the value I've set in the setElementsBelongTo() function is repeated twice?
Thank you in advance!
[Update 08/13/2015]
As a temporary workaround, I just called setElementsBelongTo() from the child subform ($fileUploadSubform) instead of the parent subform ($requestSubform)
[Update 08/17/2015]
I have tried the following code I got from http://zend-framework-community.634137.n4.nabble.com/Improved-array-support-for-Zend-Form-td667215.html as it says in that post that subform elementsTobelong to is working properly.
$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());
But here's what I'm getting for the $subForm's and $subFubForm's elements.
<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]">
[Update 08/24/2015]
I finally figured out the problem.
It was this line
$this->view->field = $additionalInfoSubform->__toString();
There were some elements not showing before that's why I added that line. And only now I understand that those elements that were not showing up are those without ViewHelper decorator set. So, when I set the ViewHelper as the decorator and remove the above fields and called setElementsBelongTo() of the subform without having to from the root of the hierarchy just from that subform, then it worked.
requestRow[1][fileUpload][fileDesc]
. Also it's probably cleaner to use$requestSubform->render()
instead of directly invoking__toString()
. – Incult