When Multiselect is used in Form API, Widget does not work. I am facing two issues:
1. Add/Remove button does not work, even button does not get render properly.

2. Form submission gives error if no item is selected in 'Selected Box' of Multiselect Form, even if attribute [#required] is set to FALSE

How to easily reproduce this issue on Drupal 7.19, PHP 5.3.x
1. download and enable multiselect module long with its FAPI Example module.
2. open form url '[ur_drupal_site]'/multiselect_fapi_example in Firefox browser.

is there any workaround available?
if not, I can work on rolling a patch.

---------------Form------------------------

$form['multiselector'] = array(
'#type' => 'multiselect',
'#title' => t('Here is the multiselect'),
'#options' => array(
'title' => t('Titles only'),
'teaser' => t('Titles plus teaser'),
'fulltext' => t('Full text'),
'1' => t('Option 1'),
'2' => t('Option 2'),
'3' => t('Option 3'),
'4' => t('Option 4'),
'5' => t('Option 5'),
'6' => t('Option 6'),
'7' => t('Option 7'),
'8' => t('Option 8'),
'9' => t('Option 9'),
'10' => t('Option 10'),
'11' => t('Option 11'),
'12' => t('Option 12'),
),
'#default_value' => _multiselect_fapi_example_options(),
'#multiple' => TRUE,
'#size' => 15,
'#required' => FALSE
);
$form = system_settings_form($form);
return $form;

------------------Form------------------------------------

Comments

tobiberlin’s picture

I want to add that theme_multiselect does not work as well. I can not override this theme function in a custom theme - and any changes in this function directly do not have any effect.

darbogast’s picture

I'm experiencing this exact issue, has there been any progress on how to fix this or how to avoid it?

nasirm’s picture

I modified the code in my local instance a long back but did not commit any patch. I changed function theme_multiselect in the module file multiselect.module and it worked for me.

function theme_multiselect($variables) {
  $element = $variables['element'];
  //Change0 Start
  $attrs = array('id', 'name', 'size', 'multiple', 'default_value');
  if (!empty($element['#required'])) {
    $attrs[] = 'required';
  }
  element_set_attributes($element, $attrs);
  //element_set_attributes($element, array('id', 'name', 'size', 'multiple', 'default_value', 'required'));

  //Change0 End

  _form_set_class($element, array('form-multiselect'));
  $options = $element['#options']; // All available options as defined by the element
  $items = $element['#default_value']; // All selected options are referred to as "items".
  $element['#field_name'] = $element['#name']; // CCK calls the #name "#field_name", so let's duplicate that..
  $required = $element['#required'];

  //Adding js/css    Change1 Start
  $path = drupal_get_path('module', 'multiselect');
  drupal_add_js($path . '/multiselect.js');
  drupal_add_css($path . '/multiselect.css');

 // Change1 End

 $widget = _multiselect_build_widget_code($options, $items, $element, $required);

  // Add a couple of things into the attributes.
  $element['#attributes']['class'][] = $widget['selfield'];
  $element['#attributes']['class'][] = "multiselect_sel";
  $element['#attributes']['id'] = $element['#field_name'];

  return $widget['prefix_pre'] . $widget['prefix_options'] . $widget['prefix_post'] . '<div class="form-item form-type-select"><select' . drupal_attributes($element['#attributes']) . '>' . _multiselect_html_for_box_options($widget['selected_options']) . '</select></div>' . "\n</div>\n";
}

nicxvan’s picture

Version: 7.x-1.9 » 7.x-1.11
StatusFileSize
new23.64 KB

This seems to still be an issue: I have attached a screenshot and my implementation is below.

 $form['field_multiselect'] = array(
                '#title' => t('Organization'),
                '#type' => 'multiselect',
                '#options' => $organization,
                '#multiple' => TRUE,
                '#size' => 15,
                '#default_value' => NULL,
            );
dsudheesh’s picture

Rolled out a patch from @nasirm.

Thanks,
Sudheesh

dsudheesh’s picture

Version: 7.x-1.11 » 7.x-1.x-dev
Status: Active » Needs review
tss’s picture

Status: Needs review » Reviewed & tested by the community

I have reviewed the patch in #5 and tested it. The multiselect works both when using it in FAPI and when using it as widget on entity fields.

rlangille’s picture

+1 RTBC

This patch helped it start working with my admin forms. Without it element looked much like the screenshot in #4 and did not function.

Should note since this is a few years later... I'm running Drupal 7.64 and multiselect 7.13, and patch works well.

tylerh111’s picture

This patch worked for me: Drupal 7.67 and Multiselect 7.13.

mparker17’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)

multiselect-7.x-1.x is no longer supported, because Drupal 7 is no longer supported.

Can you confirm if this is still an issue in multiselect-2.0.0-beta4?

I'm going to mark this issue as "Postponed (maintainer needs more info)"... but when you answer, please change it back to "Active". If there is no reply in ~6 months, then I will mark this issue as "Closed (outdated)". Thank you in advance for your understanding and patience as I try to keep this module's issue queue clear.