Well, I figured out the answer to my question by looking in the code for the _form_validate() function in form.inc. For those interested in the resolution, I hadn't unset the $form['nice_menus_number']['#options'] setting in the form, so it was still using the items in the select list as validation parameters. Once I added this line
unset($form[$form['nice_menus_number']['#options'])
to my form alter function, it worked just as I wanted.
Steve
-------- Original Message -------- Subject: "Illegal Choice" Error When Changing Select List to Text Field Date: Wed, 16 Apr 2008 13:42:37 -0700 From: Steve Edwards killshot91@comcast.net To: support@drupal.org
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