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 #4465

deselecting all multiple-select options returns default value

Details

Submitted2005-05-29 11:20 UTC
Frompear at felixdd dot de
Assignedavb
StatusClosed
PackageHTML_QuickForm
PHP VersionIrrelevant
OSIrrelevant
Roadmaps(Not assigned)

Comments

[2005-05-29 11:20 UTC] pear at felixdd dot de

Description:
------------
If one deselect all options in a multiple select element which has options checked via setDefaults the exported values still contains setDefaults values.

Run the following example and deselect all options.
After submitting you'll see, that not only the default value is in the exported values, also the display() shows the default value again. (These are two different problems.)

This issue only occours if the form contains at least a second _named_ element. Change the name of the button to null, $form->validate() returns false.

Reproduce code:
---------------
require_once 'HTML/QuickForm.php';
$form = new HTML_QuickForm('example');

$options = array('val1' => 'option 1', 'val2' => 'option 2');
$select =& $form->addElement('select', 'select', 'select', $options);
$select->setMultiple(true);

$form->addElement('submit', 'button', 'submit');
//$form->addElement('submit', null, 'submit');

$form->setDefaults(array('select' => 'val1'));

if ($form->validate()) {
echo '<pre>';
print_r($form->exportValues());
}

$form->display();

Expected result:
----------------
Array
(
[select] => Array
(
)

[button] => submit
)

or maybe null/!isset() as value for select

solution suggestion for empty array:
select.php v1.26
Line 552: $value = $this->getValue(); -> $value = array();
Line 557: $cleanValue = null; -> $cleanValue = array();

Actual result:
--------------
Array
(
[select] => Array
(
[0] => val1
)

[button] => submit
)

[2005-06-30 14:26 UTC] misc at sopic dot com

--- HTML_QuickForm-3.2.4pl1/QuickForm/select.php
2004-10-18 17:12:23.000000000 +0200
+++ HTML_QuickForm-3.2.4pl1/QuickForm/select-patched.php
2005-06-30 15:15:08.000000000 +0200
@@ -548,7 +548,8 @@
{
$value = $this->_findValue($submitValues);
if (is_null($value)) {
- $value = $this->getValue();
+ // fix for http://pear.php.net/bugs/bug.php?
id=4465
+ return $this->_prepareValue(array(), $assoc);
} elseif(!is_array($value)) {
$value = array($value);
}

[2005-07-04 23:08 UTC] markvds at elegia dot nl

With the patch from [misc at sopic dot com], the problem with the exported values is solved. But still, if the form is shown after it is processed, the default options are selected again.

[2005-07-12 23:18 UTC] people05 at sopic dot com

Yeah, I missed that one with the default options after a
reload. After digging around for some hours, here's my
solution. Warning: It's more a crude hack than a fix.

It was a little bit tricky to fix this, because
HTML_QuickForm_Select doesn't know anything about the
submitted values and passing a reference to the
HTML_QuickForm object within the constructor is difficult
without creating a BC break. So I decided to add a reference
to the HTML_QuickFrom object from the outside. Not a
beautiful solution, but it works for me. You can get the
whole patch from

http://www.sopic.com/test/QuickForm.patch

Some more warnings:

- It's possible, that my style of variable and function
naming doesn't fit your needs
- I don't know if it's working in any case
- I don't know if there's a better solution
- I don't know if there are any side effects
- I don't know if I'm creating lots of copies of
HTML_QuickForm 'cos I'm running PHP 5.

So please check the whole thing. But if it's good, it needs
to be extended. The same problem affects
HTML_QuickForm_Checkbox and maybe some others.

[2005-09-11 13:37 UTC] felic1 at gmx dot de

Maybe "broken", but still (V.3.5.2) the only solution that solves the described misbehaviour, at least if you have more than one single multiselect element on your page (and what page doesn't have...).

[2006-02-21 23:10 UTC] nigel at catalyst dot net dot nz

The fix in CVS (and in quickform 3.2.5) doesn't take into account that the form might be frozen. If it is frozen we _do_ want to display the default values.

My suggestion: change this:

if (null === $value && !$caller->isSubmitted()) {

To this:

if ($this->isFrozen() || null === $value && !$caller->isSubmitted()) {