I'm trying to use AHAH Helper to add new instances of a fieldset (containing one textfield and a group of checkboxes) to a form but I'm obviously missing something vital. The form replaces itself just fine, but no new fields are added. I suspect I need to create a counter for the number of additional field sets there are, and then iterate over the creation of them when rebuilding the form but I'm at a loss as to where those things should happen.

Unfortunately the documentation only shows how to replace one set of form elements with another - not how to create additional fields. Can anyone point me to an example of this using AHAH Helper?

Here's the code I have so far:


function createtest_input($form_state){
  $form = array();
  ahah_helper_register($form, $form_state);
  
  // construct the non-changing part of the form
  $form = define_test_form($form, $form_state);

  // add in the additional fields
  $form['additional_field_fs'] = array(
    '#type'   => 'fieldset',
    '#title'  => t('Additional fields'),
    '#prefix' => '<div id="additional-field-wrapper">', 
    '#suffix' => '</div>',
    '#tree'   => TRUE, 
  );  
  
  $form['additional_field_fs']['render'] = array(
  '#title' => t('Render '),
  '#type' => 'textfield',
  '#size' => 64,
  '#maxlength' => 255,
  '#description' => t('File path to render'),
  '#default_value' => $form_state['storage'][''additional_field_fs'']['render'],
  );
  
  $options = array(
  'first_option' => t('First option'),
  'second_option => t('Second option'),
  'third_option' => t('Third option'),
  'all' =>t('All')
  );
    
  if (isset($form_state['storage']['additional_field_fs']['invalidations']) 
        && is_array($form_state['storage']['additional_field_fs']['invalidations'])){
    $default_value = $form_state['storage']['additional_field_fs']['invalidations'];
  } else {
    $default_value = array('all');
  }
  
  $form['additional_field_fs']['invalidations'] = array(
  '#title' => t('Allowed invalidations '),
  '#type' => 'checkboxes',
  '#options' => $options,
  '#default_value' => $default_value,
  );   
  
  $form['additional_field_fs']['add_fields'] = array(
  '#type' => 'submit',
  '#value' => t('Add fields'),
  '#ahah' => array(
    'event' => 'click',
    'path' => ahah_helper_path(array(''additional_field_fs'')),
    'wrapper' => 'additional-fields-wrapper',
    'method' => 'replace',
    ),
    '#submit' => array('ahah_helper_generic_submit'),
  );
  
  $form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Create output file'),
  );
  
  return $form;
}

Thanks for any help.

Skid

Comments

jaypan’s picture

Here is a very basic example:

  ahah_helper_register($form, $form_state);
  $form['wrapper'] = array
  (
    '#prefix' => '<div id="ahah_wrapper">',
    '#suffix' => '</div>',
  );

  $form['wrapper']['checkbox'] = array
  (
    '#type' => 'checkbox',
    '#title' => t('Check this to add a field'),
	'#default_value' => $form_state['values']['checkbox'],
    '#ahah' => array
    (
      'path' => ahah_helper_path(array('wrapper')),
      'wrapper' => 'ahah_wrapper',
    ),
  );

  if($form_state['values']['checkbox'])
  {
    $form['wrapper']['extra_element'] = array
    (
      '#value' => t('This has been added'),
    );
  }

This adds the extra element if the checkbox is checked.

Contact me to contract me for D7 -> D10/11 migrations.

skidd’s picture

Thanks for that - it's all working now.