[line 20]
A few caveats: this element *must* be either created by a call to addElement on your main form (with the
construction parameters, not an element object) or you must call setParentForm on the element. If you don't, the rules on the subform won't work. Here's the two usages (assuming $form is the main form and $subForm is the sub-form):
require_once('HTML/QuickForm/SubForm.php'); $form->addElement('subForm', 'subFormElementName', 'Sub Form Label', $subForm); //NOTE: with this version $subForm is now a copy in the element so changing // $subForm now will not change the form within the element
OR
require_once('HTML/QuickForm/SubForm.php'); $el =& HTML_QuickForm::createElement('subFormElementName', 'Sub Form Label', $subForm); $el->setParentForm($form); $form->addElement($el);
This also uses a few hacks which access HTML_QuickForm internals which is a no-no, but it's the only way I could get unfreeze and setPersistentFreeze to work as HTML_QuickForm doesn't implement these functions (perhaps these should be added?). This also only works with the default QF renderer, but it shouldn't be too hard to fix it.
This *should* also work for subforms within subforms. ;-)
The following are quick instructions on how to get a dynamic subform working (i.e. a subform which is displayed / hidden by JS and conditionally validated).
Add a hidden field which holds when the sub form is displayed. $form->addElement('hidden', 'subFormDisplayed');
Use this CSS class: .hidden { overflow: hidden; visibility: hidden; display: none; }
Apply that class to a div surrounding the SubForm (I use an altered elementTemplate for QF). You also need a link which calls the javascript below (I've added it to the template for simplicity). Also, only hide it if the sub form was not displayed (if the validation fails you need to redisplay the form).
$renderer =& HTML_QuickForm::defaultRenderer(); $renderer->setElementTemplate(str_replace('{element}', '<a href="javascript:void();" onclick="showSubFormElement()">Show Sub Form</a> <div class="'.($_REQUEST['subFormDisplayed'] ? '' : 'hidden')." id="idForElementDiv">{element}</div>', $renderer->_elementTemplate), 'subFormElement');
Add the JavaScript with the function somewhere in your code.
<script language="javascript" type="text/javascript"> function newCorrectiveAction() { if(document.getElementById("idForElementDiv").className == "hidden") { document.getElementById("idForElementDiv").className = ""; document.getElementById("subFormDisplayed").value = "1"; } else { document.getElementById("idForElementDiv").className = "hidden"; document.getElementById("subFormDisplayed").value = "0"; } } </script>
Now add the sub form element and also make sure to set up the conditional validation. (assuming $subForm is your completed sub form. Note that there may be reference problems here, it's best to have the sub form finished before creating the element).
require_once('HTML/QuickForm/SubForm.php'); function subFormDisplayed($values) { return $values['subFormDisplayed'] == 1; } $el =& HTML_QuickForm::createElement('subForm', 'subFormElement', '', $subForm); $el->setPreValidationCallback('subFormDisplayed');