You can't tell a select box that it should allow multiple selectable options.
Documentation link:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

Example:

$form['dropdown'] = array(
'#type' => 'select',
'#title' => 'Select list',
'#options' => $options,
'#description' => 'Select list',
'#size' => 10,
'#multiple' => TRUE,
);

The #multiple property is never set. Issue is in the form.inc file where element_set_attributes for the select only has id, name and size as element properties.

Comments

_randy’s picture

StatusFileSize
new587 bytes

Attaching a patch for this issue. Apologies if this issue has been raised before and/or patched. I couldn't find it.

_randy’s picture

Status: Active » Closed (won't fix)

Patch above works fine to make everything multiple...
Probably best to ignore this, change the documentation to match that the #multiple can only be used in the #attributes array.

wjaspers’s picture

Why'd you close this?

Didn't #multiple adjust the #name attribute so that when the field is processed, each value is captured, instead of just one?

<select name="just_choose_one">
<option value="cookie">Cookie</option>
<option value="donut">Donut</option>
<option value="ice_cream">Ice Cream</option>
<option value="soda">Soda</option>
</select>
<select multiple="multiple" name="choose_many[]">
<option value="cookie">Cookie</option>
<option value="donut">Donut</option>
<option value="ice_cream">Ice Cream</option>
<option value="soda">Soda</option>
</select>
asimmonds’s picture

@_randy:
multiple is added to the #attributes array by form_process_select()

A select element has form_process_select() as a #process function, this is called by form_builder() as the form is built, see modules/system/system.module:424 for the form element declaration.

wjaspers’s picture

Status: Closed (won't fix) » Active

I'm puzzled then. That's what its supposed to do, but it presently doesn't or something is overriding it.

Here's what's at line 424 of modules/system/system.module. The #multiple default is still present in 7.

 ...
  $types['select'] = array(
    '#input' => TRUE,
    '#multiple' => FALSE,
    '#process' => array('form_process_select', 'ajax_process_form'),
    '#theme' => 'select',
    '#theme_wrappers' => array('form_element'),
  );
... 

And here's what's in includes/form.inc around form_process_select();
It looks as if $element['#multiple'] is interpreted as expected ...

 ...
function form_process_select($element) {
  // #multiple select fields need a special #name.
  if ($element['#multiple']) {
    $element['#attributes']['multiple'] = 'multiple';
    $element['#attributes']['name'] = $element['#name'] . '[]';
  }
  // 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.
  else {
    $required = $element['#required'];
    // 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 && !isset($element['#default_value'])) || isset($element['#empty_value']) || isset($element['#empty_option'])) {
      $element += array(
        '#empty_value' => '',
        '#empty_option' => $required ? t('- Select -') : t('- None -'),
      );
      // The empty option is prepended to #options and purposively not merged
      // to prevent another option in #options mistakenly using the same value
      // as #empty_value.
      $empty_option = array($element['#empty_value'] => $element['#empty_option']);
      $element['#options'] = $empty_option + $element['#options'];
    }
  }
  return $element;
}
... 

As far as I can see, your patch would have been correct...and should be applied. Therefore, I'm re-activating this issue.

_randy’s picture

@wjaspers -- problem was that my patch (as simple as it is) breaks all non-multiple select lists when applied. It makes everything multiple.
It could be expanded on by unsetting/altering the right attribute in the form_process_select function to fix the everything-is-multiple issue.

I got around the problem by using #multiple (to get the [] name attribute change), then used
#atrributes => array('multiple' ...) to set the multiple attribute on the element itself.

I figured this boiled down to more of a documentation clarification issue vs. anything else....

wjaspers’s picture

I guess I misunderstood the function call surrounding your change. I had mistakenly assumed it was a filter,
With that said, it looks like there was a significant change (or just missed) code in that function.

Here's what's in D6.

function theme_select($variables) {
$select = '';
  $size = $element['#size'] ? ' size="'. $element['#size'] .'"' : '';
  _form_set_class($element, array('form-select'));
  $multiple = $element['#multiple'];
  return theme('form_element', $element, '<select name="'. $element['#name'] .''. ($multiple ? '[]' : '') .'"'. ($multiple ? ' multiple="multiple" ' : '') . drupal_attributes($element['#attributes']) .' id="'. $element['#id'] .'" '. $size .'>'. form_select_options($element) .'</select>');
}

I'd generate a patch, but I don't know D7's internals well enough to say that simply porting D6's code would be accurate.

wjaspers’s picture

Status: Active » Needs review
StatusFileSize
new786 bytes

This seems to accomplish the trick!

Status: Needs review » Needs work

The last submitted patch, 1117526-multiple-property-for-select-form.inc_.patch, failed testing.

wjaspers’s picture

Status: Needs work » Needs review
StatusFileSize
new766 bytes

Lets try this again, apparently my line endings were still in Windows format.

Status: Needs review » Needs work

The last submitted patch, 1117526-multiple-property-for-select-form.inc_.patch, failed testing.

wjaspers’s picture

Version: 7.0 » 7.x-dev
Status: Needs work » Needs review
StatusFileSize
new723 bytes

Here's exactly how I fixed up theme_select(). If the attached patch doesn't work this time around, could someone please generate one that works correctly?

function theme_select($variables) {
  $element = $variables['element'];
  _form_set_class($element, array('form-select'));
  if (isset($element['#multiple']) && $element['#multiple']==TRUE) {
    $element['#attributes'] += array('multiple'=>'multiple');
    $element['#name'] .= '[]';
  }
  element_set_attributes($element, array('id', 'name', 'size'));
  return '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
}
wjaspers’s picture

Huh, latest Core -dev seems to be ok now. theme_select hasn't changed any. I'm not sure whether to change the status of or close this thread.
Seems to have risen again in 7.2.

andypost’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs tests

Is this still an issue?

thomas.fleming’s picture

StatusFileSize
new661 bytes

This issue still seems to be relevant in Drupal 8. The #multiple default is still present, and form_process_select is relatively unchanged. With that said, I rerolled the latest patch for D8.

Status: Needs review » Needs work

The last submitted patch, 15: multiple-not-set-on-selects-1117526-15.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
quietone’s picture

Issue summary: View changes
Status: Needs work » Closed (cannot reproduce)
Issue tags: -Needs tests +Bug Smash Initiative

@_randy, Thank you for reporting this problem. We rely on issue reports like this to improve Drupal core.

I tested this on Drupal 9.4.x, standard install. I used the form_api_example of the Examples module which includes a demo of #select.

    // Multiple values option elements.
    $form['select_multiple'] = [
      '#type' => 'select',
      '#title' => 'Select (multiple)',
      '#multiple' => TRUE,
      '#options' => [
        'sat' => 'SAT',
        'act' => 'ACT',
        'none' => 'N/A',
      ],
      '#default_value' => ['sat'],
      '#description' => 'Select Multiple',
    ];

And it work as expected.

Therefore, closing as cannot reproduce. If you are experiencing this problem on a supported version of Drupal reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue (starting from "Install Drupal core").

Thanks!