I’m building a form using Zend_Form and attempting to nest a sub form multiple levels deep. The result I’m after is as follows:
<div id="details">
<table class="subForm">
<tr>
<td>
<!-- Element -->
</td>
<td>
<!-- Element -->
</td>
</tr>
</table>
</div>
<div id="content">
<table class="subForm">
<tr>
<td>
<!-- Element -->
</td>
<td>
<!-- Element -->
</td>
</tr>
</table>
</div>
To do this currently, I have created a sub form with my elements and decorators, attached that to a sub form with decorators (and no elements), which is then attached to a form.
// create sub form container decorators
public $subFormContainerDecorators = array(
array('FormElements'),
array(array('data' => 'HtmlTag'), array('tag' => 'div',
'id' => 'details'))
);
// create sub form decorators
public $subFormDecorators = array(
array('FormElements'),
array(array('data' => 'HtmlTag'), array('tag' => 'table',
'class' => 'subForm'))
);
// create sub form and attach elements
$details = new Zend_Form_SubForm();
$details->setDecorators($this->subFormDecorators);
$details->addElement($name)
->addElement($save);
// create container div
$detailsContainer = new Zend_Form_SubForm();
$detailsContainer->setDecorators($this->subFormContainerDecorators);
$detailsContainer->addSubForm($details, 'details');
// attach subform container to main form
$this->addSubForm($detailsContainer, 'detailsContainer');
This is the only way I can currently seem to acheive the nested level I require. However this would also require me to make decorators for all containers and their sub forms for each sub form set. I cannot see any other way of acheiving this however I’m sure there will be a better way than this.
If anyone can help it would be greatly appreciated.
You just add the div HtmlTag decorator to $sbFormDecorators and forget about the container altogether.