| Justin Patrin 2006-04-06, 9:59 pm |
| On 4/6/06, Eric <e@chicagoskyway.org> wrote:
> 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 h=
as
> 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=3Dnull)
>
> {
>
> 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 =3D> $val) {
>
> // Warning: new API since release 2.3
>
> $this->addOption($val, $key);
>
> }
>
> return true;
>
> } // end func loadArray
>
>
>
>
>
> **Snip**
>
>
>
> function loadDbResult(&$result, $textCol=3Dnull, $valueCol=3Dnull,
> $values=3Dnull)
>
> {
>
> if (!is_object($result) || !is_a($result, 'db_result')) {
>
> return PEAR::raiseError('Argument 1 of HTML_Select::loadDbRes=
ult
> is not a valid DB_result');
>
> }
>
> if (isset($values)) {
>
> $this->setValue($values);
>
> }
>
> $fetchMode =3D ($textCol && $valueCol) ? DB_FETCHMODE_ASSOC :
> DB_FETCHMODE_DEFAULT;
>
> //my change
>
> $this->addOption('', '');
>
> while (is_array($row =3D $result->fetchRow($fetchMode)) ) {
>
> if ($fetchMode =3D=3D 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.
>
The easier way is to not edit the code and just call
$sel->addOption('', '') before you load your options.... Or even add
it to your option array (if you're using loadArray). Or use
DB_DataObject_FormBuilder which would do this for you if your field is
allowed to be null.
--
Justin Patrin
|