Download & Extend

Data being overwritten in $form_state['storage'] due to array_smart_merge() when #multiple = TRUE

Project:AHAH helper
Version:6.x-2.0
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:active

Issue Summary

When #multiple=true on select, $form_state['storage'] combines options array, instead of save currently selected items.

Comments

#1

Title:Wronga save in $form_state['storage'] for 'select' #multiple=true» Wrong save in $form_state['storage'] for 'select' #multiple=true
Priority:normal» critical

Good catch! Damn. The code was so elegant, because of this it'll have to change quite radically. Patches are welcome :)

#2

Title:Wrong save in $form_state['storage'] for 'select' #multiple=true» Wrong data saved in $form_state['storage']

#3

Title:Wrong data saved in $form_state['storage']» Data being overwritten in $form_state['storage'] due to array_smart_merge() when #multiple = TRUE
Assigned to:Anonymous» Wim Leers

The data is right. It's just being overwritten.

#4

Then, the function to be replaced is array_smart_merge() by another one that works? I can try.

#5

Assigned to:Wim Leers» Anonymous

Yes. Thanks in advance for your patch :)

#6

Looks like this is still an issue? When using a multi-page form...having to pass values via $form_state['storage'] causes same issue.

Is this something we are going to have to live with as a part of Drupal 6?

#7

I tried and failed to fix this bug in ahah_helper 6.x-2.1, but I did manage a work-around that works for me.

The solution I thought of is to remove every multiple select in $form_state['storage'] that exists in $form_state['values']. The problem with doing this in ahah_helper comes down to #tree. #tree makes it difficult to determine which entries in ['storage'] actually need to be removed.

The work-around I'm using is this:
In my form builder, before the call to ahah_helper_register() I manually prune the entries from ['storage'] that correspond to multiple select fields found in ['fields'].

Below is an example of what I did for a form that does not make use of #tree.

function module_some_form($form_state) {
  // Patchfix for an AHAH issue #375582
  $form = array();
  // Remove from storage the values for any multisel field in values.
  if (isset($form_state['storage'])) {
    $fields = array('field1', 'field2', 'field3', 'field4');
    foreach ($fields as $field) {
      if (isset($form_state['values'][$field])) {
        unset($form_state['storage'][$field]);
      }
    }
  }
  ahah_helper_register($form, $form_state);
  // ...

This works because I do know which entries in ['storage'] to remove since I know the structure of the form.
If using #tree, you will likely need to use a number of if blocks for each field instead of the foreach.

#8

For what it's worth, the work-around in #7 worked for me as well. In my case I only had one, deeply nested multiple valued field so it was almost a one liner.

<?php
 
if (isset($form_state['storage']) && isset($form_state['values']['placement']['menu']['menu_mid'])) {
    unset(
$form_state['storage']['placement']['menu']['menu_mid']);
  }
 
ahah_helper_register($form, $form_state);
?>