Home » HTML » HTML_QuickForm » Bug #3049
Can not set default selected date/time values for HTML_QuickForm_Date element
Details
| Submitted | 2004-12-29 05:28 UTC |
|---|---|
| From | musicalpath at yahoo dot com |
| Assigned | avb |
| Status | Closed |
| Package | HTML_QuickForm |
| PHP Version | 4.3.10 |
| OS | Windows XP SP2 |
| Roadmaps | (Not assigned) |
Comments
[2004-12-29 05:28 UTC] musicalpath at yahoo dot com
Description:
------------
There are actually 2 bug fixes here.
I am using the date element from HTML_QuickForm. Calling the getElements() function on my newly created element returns null instead of an array of elements.
After the fix below to getElements(), the setSelected() function for select elements incorrectly inserts the selected attribute. See code below.
Reproduce code:
---------------
Code that causes bug:
$now = getdate();
$dateoptions = array(
'format' => 'F / d / Y',
'minYear' => '2005',
'maxYear' => '1900' );
$min_date =& $form->addElement( 'date', 'date_min', 'Min Date:', $dateoptions );
$elements =& $min_date->getElements();
$elMonth =& $elements[0];
$elMonth->setSelected( $now['mon'] );
var_dump( $elements );
Here's the patch for file group.php:
/**
* Gets the grouped elements
*
* @since 2.4
* @access public
* @return array
*/
function &getElements()
{
/* Add the following 3 lines */
if (empty($this->_elements)) {
$this->_createElements();
}
return $this->_elements;
} // end func getElements
For select.php, function toHtml():
foreach ($this->_options as $option) {
if (is_array($this->_values) && in_array((string)$option['attr']['value'], $this->_values)) {
$this->_updateAttrArray($option['attr'], array('selected' => 'selected'));
}
$strHtml .= $tabs . "\t<option" . $this->_getAttrString($option['attr']) . '>' .
$option['text'] . "</option>\n";
/* add this line in the function toHtml() (Yes, this is clearly a kludge) */
$strHtml = preg_replace( "/=\"selected\"/", "", $strHtml );
}
Expected result:
----------------
For getElements()
array
0 =>
object(html_quickform_select)
var '_attributes' =>
array
'name' => 'F'
var '_tabOffset' => 0
var '_tab' => ' '
var '_lineEnd' => '
'
var '_comment' => ''
var '_label' => ''
...The rest of the dump...
For toHtml()
should output:
<option value="12" selected>December</option>
Actual result:
--------------
For getElements():
array
empty
For toHtml()
actual output:
<option value="12" selected="selected">December</option>
[2004-12-29 05:42 UTC] musicalpath at yahoo dot com
Here's a cleaner example of code that causes the problem.
$dateoptions = array(
'format' => 'F / d / Y',
'minYear' => '2005',
'maxYear' => '1900' );
$min_date =& $form->addElement( 'date', 'date_min', 'Min Date:', $dateoptions );
$elements =& $min_date->getElements();
$elements[0]->setSelected( date('m') );
$elements[1]->setSelected( date('d') );
$elements[2]->setSelected( date('Y') );
[2005-01-07 21:02 UTC] musicalpath at yahoo dot com
That is definitely a cleaner way to set the defaults (and it does work). But it has shown another possible bug.
I've got text input, multi-selection boxes, and radio buttons on this form. When I use setDefaults as you describe, the radio buttons that i've checked are unchecked. The order I'm doing things is:
create form
add radio (name1, on)
set radio checked
add radio (name1, off)
add date selection
add multi-selection boxes
set defaults for date selection
Is setDefaults() supposed to clear old default values? If so, selected values in multi-selection boxes are not getting cleared. I'm currently clearing them with the code:
selectionBoxElement->_values = null;
As for the second bug, I see your point. I'll look into it more deeply. My DOCTYPE is currently:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">