Re: Null value for select elements in QuickForm

From: Date: Fri, 07 Apr 2006 01:49:12 +0000
Subject: Re: Null value for select elements in QuickForm
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-23560@lists.php.net to get a copy of this message
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 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. > 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

« previous php.pear.general (#23560) next »