If you edit a form with this widget in, then click Preview you will find the changes you've made aren't displayed in the widget in the preview form (although they are displayed in the preview of the node, at the top of the preview page).

Why Preview Doesn't Work

I'm using a 'select' form element as the 'selected' items select box. I add and remove items from it with javascript and just as the form is submitted, javascript selects ALL the items in it: So when it's submitted, the standard code for the select box sees they're all selected and so it works.
However when "Preview" is clicked, the form that's displayed doesn't have the default nids set yet! But some how (if you use a normal select box) it still has the correct newly-selected items selected. How does it know what these are if the default nid array field isn't set?
It seems that in function form_builder (in form.inc about line 650) the #post value (from the form's submit returned values) is entered into the $form['#value'] array (about line 697).

Note that $form['#post'] = $_POST; in drupal_prepare_form (about line 291).

In form_builder:

        $edit = $form['#post']; 
        foreach ($form['#parents'] as $parent) {
          $edit = isset($edit[$parent]) ? $edit[$parent] : NULL;
        }
             .....
             .....
            case 'select':
              if (isset($form['#multiple']) && $form['#multiple']) {
                if (isset($edit) && is_array($edit)) {
                  $form['#value'] = drupal_map_assoc($edit); //THIS LINE FILLS IN THE NEWLY SELECTED ITEMS
                    //EG: $edit could equal something like Array ( [0] => 145 [1] => 1 [2] => 15 )
                }
                else {
                  $form['#value'] = array();
                }
              }
              elseif...

As these items aren't available in the select box (as they're not there at the start, because they're not selected, selecting them with the #value property won't work), the multiselect widget won't display the preview properly.

This will fairly easily break the multiselect module's preview behaviour.

I'm not sure how to fix this...
...maybe get the #post value or handle submission/preview with a special handling method.
There is also the #after_build property that might be useful.

Comments

Lionfish’s picture

Status: Active » Fixed

Preview now works: Fixed using #after_build, some alterations to how the form is made and alterations to the JS.
Tested with IE and FF, but further testing would be advisable.

Mike.

Anonymous’s picture

Status: Fixed » Closed (fixed)