Re: Bug #23746 Default values do not work when $_POST is set

From: Date: Sat, 02 Aug 2003 20:53:07 +0000
Subject: Re: Bug #23746 Default values do not work when $_POST is set
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-19158@lists.php.net to get a copy of this message
Looks like my patch was stripped by mailing list. Trying again. It is also at http://www.runningresults.freeserve.co.uk/checkbox.php.patch if it didn't work again. > I've added the following comment to Bug #23746: > > I think the current behaviour of QuickForm checkboxes is still not using > default values for checkboxes when it should be. > > The following code run on PHP 4.3.1 with version 3.0 of HTML_QuickForm ($Id: > checkbox.php,v 1.14 2003/05/16 17:21:10 avb Exp $) results in both > checkboxes being displayed unchecked. > > To reproduce, open form1.php in browser, submit the form and form2.php will > be displayed with both check boxes unticked. Opening script2.php directly > (without POST data) will result in the first checkbox being checked (correct > behaviour). > > ========form1.php======== > > <html> > <body> > <form action="form2.php" method="post"> > <input name="foo" value="random data" type="hidden"> > <input name="send" value="Send" type="submit"> > </form> > </body> > </html> > > ========form2.php======== > > <?php > > require_once ("HTML/QuickForm.php"); > $form = new HTML_QuickForm('frmTest', 'post'); > > $form->addElement('checkbox', 'test1', 'Test 1:'); > $form->addElement('checkbox', 'test2', 'Test 2:'); > > $defaultVals['test1'] = true; > $defaultVals['test2'] = false; > > $form->setDefaults($defaultVals); > $form->display(); > > ?> > > As I understand it a possible fix would be to apply the attached patch to > checkbox.php,v 1.14. As I do not fully understand QuickForm there may be > reasons why this patch is a bad idea. > > Email me if you wish to discuss the patch further. > > I am very impressed with the quality of HTML_QuickForm and would like to > assist you in making it even better. > > Thanks > Peter Jackson

--- checkbox.php Sun Jul 13 19:18:11 2003 +++ checkbox.php.mod Sun Jul 13 19:18:11 2003 @@ -257,9 +257,10 @@ // default values are overriden by submitted $value = $this->_findValue($caller->_constantValues); if (null === $value) { - // if no boxes were checked, then there is no value in the array - // yet we don't want to display default value in this case - if (isset($caller->_submitValues) && 0 < count($caller->_submitValues)) { + // use the submitted value only if a value of the + // same name was submitted, otherwise use default + if (isset($caller->_submitValues) && 0 < count($caller->_submitValues) + && $this->_findValue($caller->_submitValues) !== null) { $value = $this->_findValue($caller->_submitValues); } else { $value = $this->_findValue($caller->_defaultValues);
« previous php.pear.dev (#19158) next »