Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 3.2.16

Bug #8057 setDefaults changes radio button selection
Submitted: 2006-06-28 15:07 UTC
From: m dot verkerk at gmail dot com Assigned: avb
Status: Closed Package: HTML_QuickForm (version 3.2.6)
PHP Version: 5.1.4 OS: Linux / Windows
Roadmaps: (Not assigned)    
Subscription  


 [2006-06-28 15:07 UTC] m dot verkerk at gmail dot com (M. Verkerk)
Description: ------------ When in a form with radio-buttons that are explicitly set 'unchecked' a setDefaults command is given with any other element, the first radio button becomes selected. Workaround: set the 'value' attribute with the element creation statement to the default value. Test script: --------------- <?php require_once 'HTML/QuickForm.php'; $form = new HTML_QuickForm('firstForm'); // Add some elements to the form $r1 = &$form->addElement('radio', 'radio-1', null, "Specify:", 0, null); $form->addElement('text', 'name-1', 'Enter your name:', array('size' => 50, 'maxlength' => 255)); $r2 = &$form->addElement('radio', 'radio-1', null, "My name is John", 1, null); $form->addElement('submit', null, 'Send'); //Explicitly set unchecked $r1->setChecked(false); $r2->setChecked(false); //Set any default value $form->setDefaults(array('name-1'=>' Marijn')); if ($form->validate())exit; $form->display(); ?> Expected result: ---------------- Two unchecked radiobuttons and a textbox with default value " Marijn" Actual result: -------------- First radio button is selected

Comments

 [2006-06-29 14:40 UTC] avb (Alexey Borzov)
The problem appears because you have 0 as the value of your first radio, changing that to any other value that is not considered equal (==) to null by PHP will fix the problem. I am not quite sure whether to try to fix this or close the bug, since there is an easy workaround.
 [2006-06-29 19:25 UTC] m dot verkerk at gmail dot com
I have tried to set the default of the radiobutton to 'null'. That didn't make it work either, but it might be easier to have fixed? Thanks for the work-around!
 [2006-06-29 19:50 UTC] avb (Alexey Borzov)
OK, a bit of clarification on how this works (or rather doesn't work) and why it is difficult to fix. After setDefaults() all elements try to update their values. They search for values in submit / defaults / constants. If a value is not found it is returned as null. Now in the case of radio there is the following code: if ($value == $this->getValue()) { $this->setChecked(true); } else { $this->setChecked(false); } So null (as no value is found) is compared to the element "value" attribute (0 in our case) and they are found equal. Changing this == to === will break setting the submit value instead (the submit value will be string '0' rather than integer 0).
 [2006-07-04 12:51 UTC] m dot verkerk at gmail dot com
Thanks for the explanation!
 [2006-07-12 05:07 UTC] ohm at morishima dot net (Akitoshi MORISHIMA)
Alexey, > OK, a bit of clarification on how this works (or rather doesn't work) > and why it is difficult to fix. How about changing the code you mentioned: if ($value == $this->getValue()) { $this->setChecked(true); } else { $this->setChecked(false); } as if ($value == (string)$this->getValue()) { $this->setChecked(true); } else { $this->setChecked(false); } ?
 [2006-10-07 15:17 UTC] avb (Alexey Borzov)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better. -- Added an extra !is_null($value) check.