Form API Select List will not show empty option I am trying to add a form that forces you to select an option from a dropdown. The documentation says:


  If #required is TRUE and there is no #default_value, an empty option
  is added to the select control to force the user to make an active
  choice.


http://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#empty_value

OK that sounds great. That is what I want. So I have the following code to add the select list to my form.

$form['test'] = array(
    '#type' =&gt; 'select',
    '#options' =&gt; array (
      1 =&gt; 'Very poor',
      2 =&gt; 'Not that bad',
      3 =&gt; 'Average',
      4 =&gt; 'Good',
      5 =&gt; 'Perfect',
    ),
    '#required' =&gt; TRUE,
    '#title' =&gt; 'Rating',
  );


It is a select list, it is required and there is no default value. I however do not see a empty option...

What am I doing wrong?

Updated

The element above is now the only thing going into my array. When I dpm() the $form I get the following back:



Update 2

Below is what theme developer is seeing:



Update 3

I have set up a clean instance of drupal 7.22 and installed my custom code and the same thing is happening. I used the minimal profile. Must be something I am doing wrong else where...

Update 4

I have tested the full code on a different environment (7.19) and the same thing is happening. So it looks like there is something fundamentally wrong with the modules themselves. Will try and anonymize the problem so I can post here.

Update 5

I have the following two files in my module.

stack_test.module

&lt;?php

function stack_test_menu(){
  $items['stacktest'] = array(
    'title' =&gt; 'Stack Test',
    'page callback' =&gt; 'stack_test_form',
    'access callback' =&gt; TRUE,
    'type' =&gt; MENU_NORMAL_ITEM,
    'menu_name' =&gt; 'main-menu',
  );
  return $items;
}

function  stack_test_form(){
  $countryList = array (
    1 =&gt; t('Very poor'),
    2 =&gt; t('Not that bad'),
    3 =&gt; t('Average'),
    4 =&gt; t('Good'),
    5 =&gt; t('Perfect'),
  );
  $form['test'] = array(
    '#type' =&gt; 'select',
    '#options' =&gt; $countryList,
    '#required' =&gt; TRUE,
    '#title' =&gt; 'Rating',
  );
  return $form;
}


Then I have my stack_test.info file:

name = Stack Test
description = Test Module
package = Stack Exchange
core = 7.x

files[] = stack_test.module


Update 6

Creating a select list in a content type with validation required works as expected and '- Select a value -' is displayed at the top of the list.

Update 7

The code in Update 5 should work now. I have created a mini module just with that in and I get the same results.
It looks like you're not running your form through drupal_get_form()...that would account for the additional form processing functions not running, but you'll still get a rendered HTML element out at the other end so it could be difficult to tell the difference.

Just change the page callback for your router item, and add the form as arguments:

function stack_test_menu(){
  $items['stacktest'] = array(
    'title' =&gt; 'Stack Test',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('stack_test_form'),
    'access callback' =&gt; TRUE,
    'type' =&gt; MENU_NORMAL_ITEM,
    'menu_name' =&gt; 'main-menu',
  );
  return $items;
}


Clear the caches and give your test page another try.

The same goes for your original problem - A form is essentially just a render array, and when you return that directly from a page callback it's run through drupal_render(). That function will do what it can to render the form element, and it knows how to do it to a certain degree, but the form processing functions add a lot of extra stuff in there, which drupal_render() won't do.

Moral of the story - always use drupal_get_form() to build the output for a form :)