[development] Array of fields on an admin settings form

Randy Fay randy at randyfay.com
Fri Apr 30 21:20:04 UTC 2010


I find no problems with this code...

function form_default_some_form() {
  $default = '';
  $options = array('' => 'none', 'a' => 'a', 'b' => 'b');
  $form['myform']['mytypes'][$key] = array(
   '#type' => 'select',
   '#title' => 'Example select',
   '#options' => $options,
   '#default_value' => $default,
  );
  return $form;
}

It performs as expected on D6.16. I thought that the use of an empty string
for a key was screwy, but it doesn't seem to affect anything. (But remember,
that empty($string) will return TRUE where the string has nothing in it,
thus affecting lots of code you might not want to affect.)

Be careful that you don't have your browser tricking you by autofilling the
form. Ctrl-refresh or Shift-refresh or clear the cache. Try another browser.

-Randy

On Fri, Apr 30, 2010 at 2:41 PM, Jeff Greenberg <jeff at ayendesigns.com>wrote:

> On 4/30/2010 2:58 PM, Randy Fay wrote:
>
>> First, use an associative array for your options list, which as you've
>> already discovered is completely wrong in the code you sent out.
>> '#options' => array("orange" => t("orange"),... ) or '#options' =>
>> array('or' => t('Orange'))... or '#options' => array('1' => t('Orange'),
>> ...)
>>
>> Second, use only strings for the keys of the associative array (I've seen
>> numeric keys cause some difficulty here in D7 at least)
>>
>> You don't mention what version of Drupal you're working with.
>>
>>  Sorry. It's D6. Ok, made sure of all that, so now in the form field part,
> I have:
>
>    $form['myform']['mytypes'][$key] = array(
>      '#type' => 'select',
>      '#title' => t($key),
>      '#options' => $results_tabs,
>      '#default_value' => $results_tabs[$val]
>    );
>
> $results_tabs is an associative array, now defined as $results_tabs =>
> array(''=>'none','a'=>'a','b'=>'b');
> $key='a'
> $val='a'
>
> When I dpm $forms at the end, it shows, for example,
>    #default_value=>'a'
> and options contains the key/value pairs from $results_tabs, yet the
> dropdown when the page paints is 'none'
>



-- 
Randy Fay
Drupal Module and Site Development
randy at randyfay.com
+1  970.462.7450
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20100430/a63901eb/attachment-0001.html 


More information about the development mailing list