-
Notifications
You must be signed in to change notification settings - Fork 5
sub forms
SubForms are a very handy way to organise data. An example:
<?php
$form = new Garp_Form();
$author = new Garp_Form_SubForm();
$author->setName('Author');
$form->addSubForm($author);
$author->addElement('text', 'name');
$author->addElement('text', 'email');
?>
The submitted data from this form will look like this:
<?php
array(
'Author' => array(
'name' => 'Frits',
'email' => '[email protected]'
)
)
?>
A subform comes with its own HTML as well, elements will be wrapped in a <div class="garp-subform"></div>
.
A very important thing to do is to add the subForm as early as possible. Elements added to the subform get an ID that's constructed from all its parents. For instance, a SubForm nested 3 deep (Let's say Credentials inside Profile inside Author) will generate an id like this, for an element with name "first_name": author-profile-credentials-first_name
But only when the parents of the subform are known. This magic happens when addSubForm()
is called. So while it may feel unintuitively, try to always call addSubForm()
before any createElement()
or addElement()
calls.