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

Bug #3422 Adding radio buttons is incorrect
Submitted: 2005-02-10 08:34 UTC
From: yasheshb at gmail dot com Assigned:
Status: Bogus Package: HTML_QuickForm_Controller
PHP Version: 5.0.3 OS: fedora core 2
Roadmaps: (Not assigned)    
Subscription  


 [2005-02-10 08:34 UTC] yasheshb at gmail dot com
Description: ------------ I'm trying to set the checked attribute for the radio button element. http://www.w3.org/TR/html4/interact/forms.html#h-17.4 as per the specs above the string "checked" should work. however when i run the code below it does not work fine. if i pass attributes as a string 'checked' it does not work. please see code below. Reproduce code: --------------- <?php require_once "HTML/QuickForm.php"; require_once "HTML/QuickForm/radio.php"; $qf1 = new HTML_QuickForm('Form1', 'get'); // does not work $radio1 = new HTML_QuickForm_radio('gender', 'Gender', 'Female', 'gender_female', 'checked'); // this works fine // $radio1 = new HTML_QuickForm_radio('gender', 'Gender', 'Female', 'gender_female', array('checked' => null)); $radio2 = new HTML_QuickForm_radio('gender', null, 'Male', 'gender_male'); $qf1->addElement($radio1); $qf1->addElement($radio2); $qf1->display(); ?> Expected result: ---------------- A radio button with 'Female' option selected. Actual result: -------------- Female option of radio button is not selected.

Comments

 [2005-02-10 19:36 UTC] avb
This is the expected behaviour: when you add an element to the form, its value gets updated according to form's submit, default and constant values. In your case the form does not contain any value for the radio and thus its 'checked' attribute is removed. The array('checked' => null) approach "works" just because "no value" is represented by null.
 [2005-02-11 05:02 UTC] yasheshb at gmail dot com
Now I believe we can either use a string or an array to represent the attributes of a form element. Can you please give me an example of how the code below will work when the attibutes are passed as a string ? // does not work $radio1 = new HTML_QuickForm_radio('gender', 'Gender', 'Female', 'gender_female', 'checked'); what should the above code be changed to ? thx. yb
 [2005-02-11 07:22 UTC] avb
Once again: your code does work in the sense that radio's 'checked' attribute is set (try doing $radio1->display() to check this). But when you add the radio to the form, its 'checked' attribute is cleared because the form does not contain any value for the radio. To set this value, use setDefaults() or setConstants(). Alternatively you can set 'checked' attribute but *after* you add the radio to the form.
 [2005-02-11 08:29 UTC] yasheshb at gmail dot com
great. so basically when i use the constructor of HTML_QuickForm_radio(...) giving it a valid HTML attribute string "checked" is of no use. (pls check http://www.w3.org/TR/html4/interact/forms.html#adef-checked ) i hope this is documented in the constructor. /** * Class constructor * * @param string Input field name attribute * @param mixed Label(s) for a field * @param string Text to display near the radio * @param string Input field value * @param mixed Either a typical HTML attribute string or an associative array * @since 1.0 * @access public * @return void */ function HTML_QuickForm_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null)
 [2005-02-11 09:41 UTC] bmansion at mamasam dot com
You don't seem to understand that the checked attribute is overriden by the QuickForm object once the radio object is added to the form. In order to set a default value of an element, you use setDefaults() from the QuickForm object. In your case, you'd use: $form->setDefaults(array('gender' => 'gender_female')); I suggest you read the documentation and have a look at the examples in the /doc directory before posting any "bugs" here.
 [2005-09-23 17:11 UTC] matthieu at phpmyvisites dot net
I agree with the 'bug' author that the behaviour of pear::QF on the "checked" state of a radio input is not normal or logical. A user friendly approach would have been to allow to set "checked=checked" by simply adding a array( 'checked' => 'checked') argument to the addElement method. I can't find an easy way to do that for the moment.
 [2008-07-21 09:49 UTC] vishakhaw (Vishakha Waingankar)
// Set defaults for the form elements $form->setDefaults(array( 'sex' => 'female', )); // add radio buttons $form->addElement('radio', 'sex', 'Sex:', 'Male', 'male'); $form->addElement('radio', 'sex', '', 'Female', 'female');
 [2012-06-27 18:23 UTC] mriso (Marcos Riso)
One simply cannot set checked or not in a radio button! This Quickform stuff definitely is boring.