2

This might be a really trivial question but it is driving me nuts. I have looked at similar posts but they all address a single-select drop-down box. What I need is to get the '-None-' to show up in a multi-select box. For some reason, despite verifying my code a million times, it is not showing up. Hope someone can point out what I am doing wrong.

$form['add']['add_drop_down'] = array(
  '#type' => 'select',
  '#title' => t('Select all that apply'),
  '#options' => $options,
  '#multiple' => TRUE,
  '#required' => FALSE,
  '#empty_option' => '-None-'
);

Basically, this only shows the options in $options in the select box and doesn't add the '-None-' as the first item. It works fine if I change it to a regular single-select. The moment I turn #multiple to TRUE, the '-None-' disappears. I have tried a number of different ways, but with no luck. Hope someone can help. Thanks.

1 Answer 1

2

That behaviour is by design; from the docs for form_process_select():

// A non-#multiple select needs special handling to prevent user agents from
// preselecting the first option without intention. #multiple select lists do
// not get an empty option, as it would not make sense, user interface-wise
.

(emphasis mine).

If you want to override that behaviour you can probably implement hook_element_info_alter() and provide your own process function which does things differently to the core one. e.g.

function MYMODULE_element_info_alter(&$type) {
  $type['select']['#process'] = array('MYMODULE_form_process_select', 'ajax_process_form'),
}

function MYMODULE_form_process_select($element) {
  // Duplicate code from form_process_sleect() and change what you need.
}
1
  • I think that behavior should go for fields that are not required!! If it is a required field, then, the behavior is fine, as the user has to select an item from the multiselect. However, if it is not required and the user accidentally selected an item, but changed their mind and didn't want to select anything, then, how do they undo that without refreshing the page. Then, all the other form values would be gone. We need something to clear the select box. I think it would be just silly to add a -None- option and then validate it since we already have all the functionality right in core.
    – SN_26
    Commented Jul 17, 2013 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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