PEAR is archived and read-only

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

Home » HTML » HTML_QuickForm » Bug #8057

setDefaults changes radio button selection

Details

Submitted2006-06-28 15:07 UTC
Fromm dot verkerk at gmail dot com
Assignedavb
StatusClosed
PackageHTML_QuickForm
PHP Version5.1.4
OSLinux / Windows
Roadmaps(Not assigned)

Comments

[2006-06-28 15:07 UTC] m dot verkerk at gmail dot com

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

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

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);
}

?