Drupal – Select List “default_value”

After beating my head against the desk the past hour, I finally figured out that to set a default value on a plain select list, you need to use “#value” and not “#default_value”. So, for example, say I have an array of banks consisting of the bank id and the bank name, like so:

$banks = array(14=>"bank one", 22=>"bank two);

I then want to construct a simple select form element, this WILL NOT work:

$form['drop'] = array(
 #id' => "bank_drop",
 '#type' => 'select',
 '#title' => 'test',
 '#options' =>; $banks,
 '#default_value' => 22  // will NOT work!
);

But this works:

$form['drop'] = array(
  #id' => "bank_drop",
  '#type' => 'select',
  '#title' => 'test',
  '#options' => $banks,
  '#value' => 22  // works!
);

This was on Drupal 6.15

Hope that saves someone some headache :)

12 thoughts on “Drupal – Select List “default_value”

  1. Well, this is nice. But then if a user changes the SELECT box, the new value is ignored on submit.

  2. Don’t know whatcha doin’ there ethan, but all the above is doing is spitting out an html form with an option preselected. Sounds like something else is happening post-submit.

  3. Thanks! This has been the answer to my issue for the last couple of hours… Drupal’s Form API is misleading on this front. It feels like a bug…

Comments are closed.