[support] "Illegal Choice" Error When Changing Select List to Text Field

Steve Edwards killshot91 at comcast.net
Wed Apr 16 20:42:37 UTC 2008


I'm using the Nice Menus module, and I would like to be able to have 
more than the 10 menus allowed by the module.  This limit is set by a 
hardcoded select list on the admin settings page for the module.  I want 
to modify the form by changing the select list to a text field so that 
an arbitrary number of menus can be selected.  To do this, I'm using 
hook_form_alter() to modify the nice_menus_number field in the 
nice_menus_admin_settings form.  First here is the form code in the module

/**
 * Settings form as implemented by hook_menu
 */
function nice_menus_admin_settings() {
  $form['nice_menus_number'] = array(
    '#type' => 'select',
    '#title' => t('Number of Nice Menus'),
    '#description' => t('The total number of independent nice menus 
(blocks) you want.'),
    '#default_value' => variable_get('nice_menus_number', '2'),
    '#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
  );

  return system_settings_form($form);
}

and here is my form alter function:

function cwl_form_alter($form_id, &$form) {
  switch ($form_id) {
      case 'nice_menus_admin_settings':
         $form['nice_menus_number']['#type'] = 'textfield';
         $form['nice_menus_number']['#size'] = 2;        
          break;
    }
}


This works in that it changes the field type to a text field, and the 
value attribute is the value in the nice_menus_number variable.  This 
works fine as long as I select a value of 10 or less.  However, if I 
select a value higher than 10, I get an error that says " An illegal 
choice has been detected. Please contact the site administrator."

But, if I change my function to also use a select list, like this:

function cwl_form_alter($form_id, &$form) {
  switch ($form_id) {
      case 'nice_menus_admin_settings':
           $form['nice_menus_number']['#options'] = 
drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
15));
             break;
    }
}

it works fine, and I can select a value higher than 10.  Since it's 
using the default system_settings_form() function, there isn't a maximum 
value set somewhere.  Plus, the default value is being set correctly 
when the form is initially displayed.  Can anybody explain why a select 
list works, but a text field doesn't?

Thanks.

Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20080416/63ccfe28/attachment.htm 


More information about the support mailing list