4

I'm building a form in a module using the Drupal Form API (Drupal 6) and have it mostly working, however I have a few required fields but don't know how to set a blank or null first selection option so the user will be forced to select an option. Here's an example of my code using my country select box:

$form['address']['country'] = array(
    '#type' => 'select',
    '#title' => 'Country of Residence',
    '#options' => get_country_form_array(),
    '#required' => true
);

Here's the get_country_form_array() function:

function get_country_form_array() {

    $countries = variable_get('form_countries');

    $output[0] = 'Select a Country';

    foreach ($countries as $country) {

        $output[$country[0]] = $country[1];

    }

    return $output;

}

How do I add a blank first option to force a selection by the user?

  • I'm using $output[0] = 'Select a Country'; as a temporary way of setting the first value, but this counts as a value so the user isn't being forced to select a country. – PHLAK Aug 15 '12 at 15:57
3

redhatlab has the right answer. If it is not working as you'd expect just add a validate handler to check the value yourself:

function MYMODULE_form_function_name_validate($form, &$form_state) {
  if ($form_state['values']['country'] == 0) {
    form_set_error('country', t('Please select a country'));
  }
}
  • This is what I ended up having to do. – PHLAK Aug 15 '12 at 17:23
4

In Drupal 7, you could use the #empty_option property, for which the default value is "- Select -" for a required field, and "- None -" for an optional field. The property adds the additional label as per the following cases:

  • 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.
  • If #empty_value or #empty_option is set and #required is FALSE (default), an empty option is added to the select control, allowing the user to choose nothing.
  • If none of #required, #empty_value, #empty_option, and #default_value are set, then no empty option is added to the select control. This leaves the control in a slightly illogical state, since all user agents automatically preselect the first available option. There's no way for the user to select nothing, and the user is also not forced to make an active decision.

The property is used only for "select" form fields.

2

You should use the default value (#default_value)

$form['address']['country'] = array(
    '#type' => 'select',
    '#title' => 'Country of Residence',
    '#options' => get_country_form_array(),
    '#required' => true,
    '#default_value' => 0
);

Here is the part I missed the first time:

In your validation function for the form you will then do:

function name_yourform_validate($form, &$form_state) {
  if (($form_state['values']['country'] == '') || ($form_state['values']['country'] == 0)) {
    form_set_error('country', t('You must select a country.'));
  }
}
  • This doesn't seem to have changed anything. I'm still able to submit the form with the default value "Select a Country" selected. Obviously this isn't a country so I want to force them to make a selection. – PHLAK Aug 15 '12 at 16:40
  • I just realized I miss a part on the answer, please take another look. – Emil Orol Aug 15 '12 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.