I have used multiselect as a part of fields in my content type. When the validation fails while submitting the node form the values that were selected in the multiselect widget is lost. Is there any patch to solve this.

Comments

okeedoak’s picture

I'm having the same problem.

okeedoak’s picture

Status: Active » Closed (duplicate)

This seem to be the same as Widget broken when form validations fails

acolyte26’s picture

I managed to get the widget broken issue fixed apply the supplied patch but still getting this issue.

spadxiii’s picture

Version: 7.x-1.8 » 7.x-1.x-dev
Status: Closed (duplicate) » Active

This doesn't seem to be a duplicate of #1101264: Widget broken when form validations fails which is about missing css/js. This issue is about losing the selected options.

I'm running into this issue right now and am looking for a (possible) solution.

spadxiii’s picture

I've made a little fix to get it working. Added an after-build where the html is rebuilt with the correct selection.

Not sure if this is the most optimal way

pierrepaul’s picture

Status: Active » Reviewed & tested by the community

Patch in #5 worked for me. Had to apply it by hand, but it might be a mistake on my side or because I had already another patch for multiselect.

Thanks!

alexweber’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for the great work! This has been committed in 8e8bedc.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

tobiberlin’s picture

Just a hint for people who may use this widget within a profile2 entity: profile2 fields are nested into an array named "profile_PROFILE_NAME" within the form_state array. So to make this work I changed the function in the above patch:

 /**
 * After_build callback for this widget.
 */
function _multiselect_after_build($element, $form_state) {
 
  $value_key = $element['#columns'][0];
  $items = array();
  
  // for profile2 fields
  if ($element['#entity_type'] == 'profile2') {
  $profile_array = 'profile_'. $element['#bundle'];
      if (isset($form_state['values'][$profile_array])) {  
          if (isset($form_state['values'][$profile_array][$element['#field_name']][LANGUAGE_NONE])) {  
                foreach ($form_state['values'][$profile_array][$element['#field_name']][LANGUAGE_NONE] as $nid) {
                  $items[] = array($value_key => $nid);
                }
          }  
      }
  } 
  
  // standard for all other fields 
  else {
      if (isset($form_state['values'][$element['#field_name']][LANGUAGE_NONE])) {
        foreach ($form_state['values'][$element['#field_name']][LANGUAGE_NONE] as $nid) {
          $items[] = array($value_key => $nid);
        }
      }
  }

  $widget = _multiselect_build_widget_code($element['#options'], $items, $element, $element['#required']);

  $element['#prefix'] = $widget['prefix_pre'] . $widget['prefix_options'] . $widget['prefix_post'];

  return $element;
}
mwesthof’s picture

Note that the suggested patch breaks any hook_form_alter() or hook_field_widget_form_alter() implementations where the #prefix is also modified. As the patch causes the #prefix value to be refilled after the form post.

As this module 'relies' heavily on the form element's #prefix value, I'm unsure how to cleanly fix the addressed problem without being in the way of other modules wanting to add something to the #prefix value.