HTML_QuickForm Data Persistance interfering with dynamic values

From: Date: Wed, 06 Oct 2004 20:01:38 +0000
Subject: HTML_QuickForm Data Persistance interfering with dynamic values
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-14778@lists.php.net to get a copy of this message
This is a long question, so bear with me. I'm starting to get really stuck here... I have a form that modifies "item" properties in my database. The form's first page is initialized by passing a GET argument to the php script. Example: myhost.com/editItem.php?itemId=5 The item id is put into a hidden element, which is repeatedly accessed throughout the form. QFC data persistence is interfering with the hidden element's value. I am using a wizard style multi-page form provided by HTML_QuickForm_Controller. This hidden element is set to a new value on each form invocation and it stays the same throughout the form. But, the form is going to be run many times over and over. The hidden element will be different each time. AFAIK, QFC is treating the the hidden element's value (which is set during creation) as a default value, and this is value is overridden by the value used on the form's last invocation. ( In other words, last time the form was run, a different value was used. This value is preserved in session variables, as described and intended by the QFC and QF framework. ) The editItem.php page holds code for a QFC form. <SIDE NOTE> I have chosen this method because I'm linking to this page from another form...it is very handy. I could use this (editItem.php) form to choose the item, then I would not need the hidden element, but that is not my intention at all. </SIDE NOTE> Here is my code snippet: Word wrap kills readability, but paste this into another page and I think it fixes itself. <START CODE SNIPPET> //Invoked like this: editItem.php?itemId=5 require_once 'HTML/QuickForm/Controller.php'; require_once 'HTML/QuickForm/Action/Display.php'; session_start(); class Page1 extends HTML_QuickForm_Page { function buildForm() { $this->_formBuilt = true; /* I use a session variable here because when user * returns to this page via a back button, * the GET variable no longer exists. */ if( $_GET['itemId'] ) $_SESSION['odbItemId'] = $_GET['itemId']; $this->addElement('hidden', 'odbItemId', $_SESSION['odbItemId'] ); $this->addElement('text', 'text1', 'Text 1' ); $button[] =& HTML_QuickForm::createElement( 'submit', $this->getButtonName('back'), '<< Back' ); $button[] =& HTML_QuickForm::createElement( 'submit', $this->getButtonName('next'), 'Next Step >>' ); $this->addGroup( $button, 'buttons', '', '&nbsp', false ); $this->setDefaultAction('next'); } } class Page2 extends HTML_QuickForm_Page { function buildForm() { $this->_formBuilt = true; $this->addElement('text', 'text2', 'Text2' ); $button[] =& HTML_QuickForm::createElement( 'submit', $this->getButtonName('back'), '<< Back' ); $button[] =& HTML_QuickForm::createElement( 'submit', $this->getButtonName('next'), 'Next Step >>' ); $this->addGroup( $button, 'buttons', '', '&nbsp', false ); $this->setDefaultAction('next'); } } class ActionProcess extends HTML_QuickForm_Action { function perform(&$page, $actionName) {
        $values = $page->controller->exportValues();
        echo '<pre>';
        var_dump($values);
        echo '</pre>';
} } $wizard = new HTML_QuickForm_Controller('asdfj111111'); //my pseudo-random string :) $wizard->addPage( new Page1('page1') ); $wizard->addPage( new Page2('page2') ); $wizard->addAction('process', new ActionProcess() ); $wizard->run(); </END CODE SNIPPET> After the page loads, I do a "view source" of the resulting HTML and initially the hidden element is set exactly as desired. The form works wonderfully. After moving through the form's steps and finishing, I go to run it again at a later time. **This time initializing it with a different itemId GET variable.** The page appears to load correctly, but I view the HTML source and see that the hidden element contains the value used previously. NOT THE NEW VALUE. I did a little debugging and my GET/SESSION step is working fine. So, I have tried several ways to circumvent this behavior. None have worked 100% yet. Does anybody have any suggestions? Here is what I have tried. I have tried to modify the container array directly: <CODE FIX> $data =& $this->controller->container(); $data['values']['page1']['odbItemId'] = $_SESSION['odbItemId']; </CODE FIX> This almost works. Initially the hidden value is wrong, but If I refresh the page, it changes the value. I can't have users refresh the page each time... I have also tried to modify the page's default values, by calling $this->setDefaults($defaults) this doesn't work. I tried using $this->setConstants($constants) but this is no better. I do have one fix that does work. By changing the HTML_QuickForm_Controller($name) to be different upon initialization it would erase ALL defaults. The form would have new SESSION state variables. THIS DOES WORK. Calling the script would look like this: editItem.php?itemId=5&name=RANDOMSTRING where RANDOM STRING could be the name for my form. This leads to many many session variables hanging around. Each time the form is initialized, another session variables gets left behind. Is this bad? It seems it would get out of hand soon... In my small amount of usage I started to have 10 to 20 "SESSION containers" hanging around. Before I start going in this direction, I want to see what others had to say. I thank you all for your help in advance!! -- Ben Ford Bio-Logic Aqua Technologies 5001 Lower River Rd Grants Pass, OR 97526 800-FOR-MIST (367-6478) ben@naturestears.com

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