PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » HTML » HTML_QuickForm_Controller » Bug #3422

Adding radio buttons is incorrect

Details

Submitted2005-02-10 08:34 UTC
Fromyasheshb at gmail dot com
StatusBogus
PackageHTML_QuickForm_Controller
PHP Version5.0.3
OSfedora core 2
Roadmaps(Not assigned)

Comments

[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.

[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 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.