Is it valid to use entries in #default_value if they are not in the #options? I have a form. On this form I have a select element, like this:

  array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('Animals'),
    '#multiple' =&gt; TRUE,
    '#options' =&gt; array(
      'rat' =&gt; t('Rat'),
      'cat' =&gt; t('Cat'),
      'dog' =&gt; t('Dog'),
    ),
  );


I also have a function called get_suggested_animals($user_id). Let's assume that this code works:

global $user;
print_r(get_suggested_animals($user-&gt;uid));

// Array
// (
//    [0] =&gt; lizard
//    [1] =&gt; cat
// )


Would it be OK to just write:

array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('Animals'),
    '#multiple' =&gt; TRUE,
    '#options' =&gt; array(
      'rat' =&gt; t('Rat'),
      'cat' =&gt; t('Cat'),
      'dog' =&gt; t('Dog'),
    ),
    '#default_value' =&gt; get_suggested_animals($user-&gt;uid),
  );


without testing if default options are on the list of available ones? I know it works (in this example - only 'cat' is selected by default), it does not generate any warnings or anything, but I still feel uncertain if it's legit, or if there are hidden traps I simply don't know yet.
Reading through the code of form_process_select it is obvious that the situation where #default_value is set, but the value isn't a valid option, is not being handled properly:

This piece of code may "fail" in that case:

// If the element is required and there is no #default_value, then add an
// empty option that will fail validation, so that the user is required to
// make a choice. Also, if there's a value for #empty_value or
// #empty_option, then add an option that represents emptiness.
if (($required &amp;&amp; !isset($element['#default_value'])) || isset($element['#empty_value']) || isset($element['#empty_option'])) {
  $element += array(
    '#empty_value' =&gt; '',
    '#empty_option' =&gt; $required ? t('- Select -') : t('- None -'),
  );


In your sample code, you don't set #required for the select field. That is why you don't observe any effect.

However, in the general case, you can't supply a bogus value for #default_value and always be sure that theFAPI will treat it just the same as not set or NULL #default_value.

If change your example and set #required to TRUE, and #multiple to FALSE, you should be able to reproduce a situation where having a bogus default value (such as "lizard") behaves different from the default value being NULL or absent.