HTML_QuickForm_Controller -> is there an easy way to kind of freeze() the forms

From: Date: Wed, 28 Jul 2004 09:23:20 +0000
Subject: HTML_QuickForm_Controller -> is there an easy way to kind of freeze() the forms
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-13880@lists.php.net to get a copy of this message
Hi there! The problem: I have to create many complex, wizard-like forms spread on multiple pages, so HTML_QuickForm and HTML_QuickForm_Controller seemed to be the right solution for me. The last step of some forms should be a kind of confirm page, where all the data the user entered is displayed. In HTML_QuickForm I would do this with $myForm->freeze(), but with HTML_QuickForm_Controller this does not seem to work. My approach was based on the regWizard example in the documentation: build the first two pages and copy the buildForm code in the buildForm of the confirmation Page and then set the defaults for the confirmation page from the $_SESSION array. The first time the confirmation page is displayed this will work, but if I go back and change some data it ill not update the defaults. Any hints appreciated. Robert Sorry for the large amount of code. -----------------------------------------8<------------------------------------------------ <?php /** * * * $Id: regWizard_2.php * * */ require_once 'HTML/QuickForm/Controller.php'; require_once 'HTML/QuickForm/Action/Display.php'; // Start the session session_start(); // Class for first page : credentials class Page_Account_Credentials extends HTML_QuickForm_Page { function buildForm() {
       $this->_formBuilt = true;
       $this->addElement('header', 'credential_header', 'Your credentials');
            $this->addElement('text', 'username', 'Your email address :', array('size' => 30, 'maxlength' => 63));
           $buttons[0] =& HTML_QuickForm::createElement('button', 'cancel', 'Cancel', array('onclick'=>"javascript:location.href='http://pear.php.net/package/HTML_QuickForm';"));
       $buttons[1] =& HTML_QuickForm::createElement('submit', $this->getButtonName('next'), 'Next step >>');
       $this->addGroup($buttons, 'buttons', '', '&nbsp', false);
       $this->setDefaultAction('next');
} } // Class for second page : user data class Page_Account_Information extends HTML_QuickForm_Page { function buildForm() {
       $this->_formBuilt = true;
       $this->addElement('header', 'data_header', 'Your personal data');
            $name[] = &HTML_QuickForm::createElement('text', 'first', 'Firstname', array('size' => 16, 'maxlength' => 63));
       $name[] = &HTML_QuickForm::createElement('text', 'last', 'Lastname', array('size' => 16, 'maxlength' => 63));
       $this->addGroup($name, 'name', 'Your name :', null, false);
            $this->addElement('text', 'company', 'Your company :',  array('size' => 30, 'maxlength' => 63));
       $prevnext[] =& $this->createElement('submit',   $this->getButtonName('back'), '<< Previous step');
       $prevnext[] =& $this->createElement('submit',   $this->getButtonName('next'), 'Next');
       $this->addGroup($prevnext, 'buttons', '', '&nbsp;', false);
            $this->setDefaultAction('next');
} } // Class for third page: confirm Data class Page_Confirm_Information extends HTML_QuickForm_Page { function buildForm() {
       $this->_formBuilt = true;
       $this->setDefaults($_SESSION['_regWizard_container']['values']['page1']);
       $this->setDefaults($_SESSION['_regWizard_container']['values']['page2']);
            $this->addElement('header', 'credential_header', 'Your credentials');
            $this->addElement('text', 'username', 'Your email address :', array('size' => 30, 'maxlength' => 63));
       $this->addElement('header', 'data_header', 'Your personal data');
            $name[] = &HTML_QuickForm::createElement('text', 'first', 'Firstname', array('size' => 16, 'maxlength' => 63));
       $name[] = &HTML_QuickForm::createElement('text', 'last', 'Lastname', array('size' => 16, 'maxlength' => 63));
       $this->addGroup($name, 'name', 'Your name :', null, false);
            $this->addElement('text', 'company', 'Your company :',  array('size' => 30, 'maxlength' => 63));
           $prevnext[] =& $this->createElement('submit',   $this->getButtonName('back'), '<< Previous step');
       $prevnext[] =& $this->createElement('submit',   $this->getButtonName('next'), 'Finish');
       $this->addGroup($prevnext, 'buttons', '', '&nbsp;', false);
       $this->setDefaultAction('next');
       $this->freeze();
       }
 }
// Class for form rendering class ActionDisplay extends HTML_QuickForm_Action_Display { function _renderForm(&$page) {
       $renderer =& $page->defaultRenderer();
       $page->setRequiredNote('<font color="#FF0000">*</font> shows the required fields.');
       $page->setJsWarnings('Those fields have errors :', 'Thanks for correcting them.');
            $renderer->setFormTemplate('<table width="450" border="0" cellpadding="3" cellspacing="2" bgcolor="#CCCC99"><form{attributes}>{content}</form></table>');
       $renderer->setHeaderTemplate('<tr><td style="white-space:nowrap;background:#996;color:#ffc;" align="left" colspan="2"><b>{header}</b></td></tr>');
       $renderer->setGroupTemplate('<table><tr>{content}</tr></table>', 'name');
       $renderer->setGroupElementTemplate('<td>{element}<br /><span style="font-size:10px;"><!-- BEGIN required --><span style="color: #f00">*</span><!-- END required --><span style="color:#996;">{label}</span></span></td>', 'name');
       $page->accept($renderer);
       echo $renderer->toHtml();
} } // Class for form processing class ActionProcess extends HTML_QuickForm_Action { function perform(&$page, $actionName) {
       $values = $page->controller->exportValues();
       $this->unsetSessionData(array(''));
        }
    function unsetSessionData($exclusion_array){
       foreach ( $_SESSION as $key=>$value ) {
           if ((!in_array($key, $exclusion_array ))) {
               unset ($_SESSION[$key]);
           }
       }
} } $wizard =& new HTML_QuickForm_Controller('regWizard', true); $wizard->addPage(new Page_Account_Credentials('page1')); $wizard->addPage(new Page_Account_Information('page2')); $wizard->addPage(new Page_Confirm_Information('page3')); $wizard->setDefaults(array('country' => 'FR')); $wizard->addAction('display', new ActionDisplay()); $wizard->addAction('process', new ActionProcess()); $wizard->run(); ?> <PRE> <?php print_r('$_SESSION' . "\n"); print_r($_SESSION['_regWizard_container']['values']['page1']); print_r('$_SESSION' . "\n"); print_r($_SESSION['_regWizard_container']['values']['page2']); ?> </PRE> -----------------------------------------8<------------------------------------------------

« previous php.pear.general (#13880) next »