Hello,

I would like to create a form to register groups. Members of the group should be added via AHAH (just like the choices in the poll module).
While I have started with the poll module code, the add button don't work correctly.

When first loading the page, the 3 fields (numbered 0, 1, 2) are displayed correctly. After the first click on the add button, only the field numbered 3 is displayed. Afterwards a field 1 and 2 are added after the next 2 clicks, and then it remains blocked with these 3 fields.

Do you have any idea why it is behaving like that ?

Vincent

P.S. : Here is a fragment of the code :

<?php
function x_group_form($form_state){
  ...
  if (isset($form_state['members_count'])) {
    $members_count = $form_state['members_count'];
  }
  else {
    $members_count = max(1, empty($form_state['values']) ? 3 : count($form_state['values']['member']));
  }

  // Add a wrapper for the members and more button.
  $form['members']['member_wrapper'] = array(
    '#tree' => FALSE,
    '#weight' => -4,
    '#prefix' => '<div class="clear-block" id="group-members-wrapper">',
    '#suffix' => '</div>',
  );

  // Container for just the group members.
  $form['members']['member_wrapper']['member'] = array(
    '#prefix' => '<div id="group-members">',
    '#suffix' => '</div>',
    '#theme' => 'group_members',
  );

  // Add the current members to the form.
  for ($delta = 0; $delta < $members_count; $delta++) {
    $person   = isset($form_state['values']['member'][$delta]['person'])   ? $form_state['values']['member'][$delta]['person'] : '';
    $function = isset($form_state['values']['member'][$delta]['function']) ? $form_state['values']['member'][$delta]['function'] : '';
    $bdate    = isset($form_state['values']['member'][$delta]['bdate'])    ? $form_state['values']['member'][$delta]['bdate'] : array();
    $edate    = isset($form_state['values']['member'][$delta]['edate'])    ? $form_state['values']['member'][$delta]['edate'] : array();

    $form['members']['member_wrapper']['member'][$delta] = _group_member_form($delta, $person, $function, $bdate, $edate);
  }

  // We name our button 'member_more' to avoid conflicts with other modules using
  // AHAH-enabled buttons with the id 'more'.
  $form['members']['member_wrapper']['member_more'] = array(
    '#type' => 'submit',
    '#value' => t('More members'),
    '#ahah' => array(
      'path'    => 'db/assoc/js',
      'wrapper' => 'group-members',
      'method'  => 'replace',
      'effect'  => 'fade',
    ),
  );
  ...
}

/**
 * Menu callback for AHAH additions.
 */
function age_group_member_js() {

  $delta = count($_POST['member']);

  // Build our new form element.
  $form_element = _group_member_form($delta);

  // Build the new form.
  $form_state = array('submitted' => FALSE);
  $form_build_id = $_POST['form_build_id'];
  // Add the new element to the stored form. Without adding the element to the
  // form, Drupal is not aware of this new elements existence and will not
  // process it. We retreive the cached form, add the element, and resave.
  if (!$form = form_get_cache($form_build_id, $form_state)) {
    drupal_set_message(t('Unable to recover cached form'),'error');
    exit();
  }
  $form['member_wrapper']['member'][$delta] = $form_element;
  form_set_cache($form_build_id, $form, $form_state);

  $args = $form['#parameters'];
  $form_id = array_shift($args);
  $form_state['post']  = $form['#post']     = $_POST;
  $form['#programmed'] = $form['#redirect'] = FALSE;

  //drupal_process_form($form_id, $form, $form_state);

  // Rebuild the form.
  $form = form_builder($form_id, $form, $form_state);

  // Render the new output.
  $member_form = $form['member_wrapper']['member'];
  unset($member_form['#prefix'], $member_form['#suffix']); // Prevent duplicate wrappers.
  $member_form['#theme'] = 'group_members'; // should not be necessary
  $member_form[$delta]['#attributes']['class'] = empty($member_form[$delta]['#attributes']['class']) ? 'ahah-new-content' : $member_form[$delta]['#attributes']['class'] .' ahah-new-content';
  $output = theme('status_messages') . drupal_render($member_form);

  drupal_json(array('status' => TRUE, 'data' => $output));
}
?>

Comments

vmal’s picture

no idea ?