Null value for select elements in QuickForm
| From: | Eric | Date: | Thu, 06 Apr 2006 20:43:32 +0000 |
| Subject: | Null value for select elements in QuickForm | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-23559@lists.php.net to get a copy of this message | ||
Hello,
I am using PEAR's HTML library to create a form that has many select
elements on it. I want a default value that will indicate that the user has
not taken action and selected anything from the list. For example, the
select defaults to a blank option with a null value. I could not find an
easy way to do this so I made my own element that was just the select.php
file with the following changes.
**Snip**
function loadArray($arr, $values=null)
{
if (!is_array($arr)) {
return PEAR::raiseError('Argument 1 of HTML_Select::loadArray is
not a valid array');
}
if (isset($values)) {
$this->setSelected($values);
}
//my change
$this->addOption('', '');
foreach ($arr as $key => $val) {
// Warning: new API since release 2.3
$this->addOption($val, $key);
}
return true;
} // end func loadArray
**Snip**
function loadDbResult(&$result, $textCol=null, $valueCol=null,
$values=null)
{
if (!is_object($result) || !is_a($result, 'db_result')) {
return PEAR::raiseError('Argument 1 of HTML_Select::loadDbResult
is not a valid DB_result');
}
if (isset($values)) {
$this->setValue($values);
}
$fetchMode = ($textCol && $valueCol) ? DB_FETCHMODE_ASSOC :
DB_FETCHMODE_DEFAULT;
//my change
$this->addOption('', '');
while (is_array($row = $result->fetchRow($fetchMode)) ) {
if ($fetchMode == DB_FETCHMODE_ASSOC) {
$this->addOption($row[$textCol], $row[$valueCol]);
} else {
$this->addOption($row[0], $row[1]);
}
}
return true;
} // end func loadDbResult
**Snip**
Does anyone know if this will make anything in QuickForm explode? So far it
has been working for me, but I am nervous that it will explode. Also, if
there is an easier way to do this please clue me in.
Thanks,