QuickForm Controller, Defaults, and Dynamic Form Elements
| From: | Michael C | Date: | Tue, 12 Sep 2006 18:11:50 +0000 |
| Subject: | QuickForm Controller, Defaults, and Dynamic Form Elements | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-25110@lists.php.net to get a copy of this message | ||
Hi there,
I've recently gotten into the wonderful world of quickform controller,
and have run into an issue with my perhaps unconventional use of the
package. I'm trying to build out a form builder wizard that includes
client side dynamically created form elements on the quickform pages.
IE: Instead of doing a page reload, the user just clicks an "add"
button and then s/he has a new field where they can define a new form
element with no page reload. I then am trying to auto detect server
side what the new form elements are, and then adding the corresponding
quickform elements. _Trying_ is the keyword here :)
Overall, my approach has worked a-okay, however I am having issues
rebuilding the form pages from scratch using the controller
setDefaults().
The below controller page example show how I have gone about
connecting client side genereted form elements, with server side
quickform elements. I am using a combination of looking at the _POST
data, or the result of $this->controller->exportValues(). I have
found that if I call $this->controller->exportValues() when new client
side form elements are added, it does not pick up their values. I
assume because the elements don't exist at the quickform level at the
time of a new POST, $this->controller->exportValues() just doesn't see
them (makes sence). So, I look at the raw _POST data when a new post
has been made to the page, and $this->controller->exportValues() when
the page is simply just being loaded:
class PageFirst extends HTML_QuickForm_Page
{
function buildForm()
{
$this->_formBuilt = true;
$set_group = false;
if ($_POST[$this->getButtonName('next')]) {
$values = $_POST;
} else {
$values = $this->controller->exportValues();
}
// Add client side dynamically created form elements
if (!empty($values)) {
while (list($key,) = each($values)) {
if (strpos($key, 'field_') !== false) {
list(, $field_count) = explode('_', $key, 2);
if (is_numeric($field_count)) {
$this->_addFieldGroup($field_count);
$set_group = true;
}
}
}
}
// otherwise, just add one field group
if (!$set_group) {
$this->_addFieldGroup();
}
$this->addElement('button', 'add', 'Add', array('onclick' =>
'add_field()'));
$this->addElement('submit', $this->getButtonName('cancel'), 'Cancel');
$this->addElement('submit', $this->getButtonName('next'), 'Next >>');
$this->setDefaultAction('next');
}
function _addFieldGroup($ID = 1)
{
$element = array();
$element['name'] =& $this->createElement('text', 'name', null,
array('size' => 30));
$element['type'] =& $this->createElement('select',
'element_type', null, $GLOBALS['_types']);
$element['required'] =& $this->createElement('checkbox',
'required', 'Required');
$this->addGroup($element, 'field_' . $ID);} } This approach has worked well for me, and has allowed me to do some other cool stuff. For example, on the next page, I then go ahead server side to build out a custom form based on the dynamically generated client side data from the previous page. The point where I have gotten into a bind is when the user comes back and wants to go through the wizard again to edit previously created content. My approach here has totally backfired. I do something like this: $wizard =& new HTML_QuickForm_Controller('Wizard', true); $wizard->addPage(new PageFirst('PageFirst')); $wizard->addPage(new PageSecond('PageSecond')); .... $defaults = ...; $wizard->setDefaults($defaults); $wizard->run(); My hope here is when I call $this->controller->exportValues() I'll get a result set inclusive of the default data, and then I can go on my merry way and build out the dynamic form. So much for that theory.... Any ideas on how I can accomplish this? Thanks, Michael