Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

This is my custom form with working add more and dummy delete button.

function mymodule_mycustom_form($form, $form_state){    
   $form['checkboxes_fieldset'] = array(
    '#prefix' => '<div id="checkboxes-div">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#description' => t('This is where we get automatically generated textfield'),
    '#tree' => TRUE,
  );
  $form['checkboxes_fieldset']['number'] = array(
      '#type' => 'textfield',
      '#title' => 'number',
    );
  // dummy delete button    
  $form['checkboxes_fieldset']['delete'] = array(
      '#type' => 'submit',
      '#value' => 'delete',
  );
  $form['add'] = array(
    '#value' => t('add more?'),
    '#type' => 'submit',
    '#ajax' => array(
      'callback' => 'mymodule_add_callback',
      'wrapper' => 'checkboxes-div',
      'method' => 'append',
    ),  
  );

  return $form;
}

This is ajax callback for add more button

function mymodule_add_callback($form, $form_state) {
  return $form['checkboxes_fieldset'];
}

Now i want a working delete. But dont know how?

share|improve this question

1 Answer

Adding a value using that code won't actually work since you've only defined one textfield, and thus, only one value will be stored. All the new fields returned will have the same id/name as the first one so only one form value will be sent back to the server when the form is submitted.

You need to add an extra layer to the form structure to make each field instance unique, say by including an instance number in each id/name. Something like $form['checkboxes_fieldset'][$i]['number'] where $i is increased by one for each field instance. That goes for all the elements inside the fieldset. You generate these fields in a loop, but before that you check how many instances of the field are already present in the form state so you know how many iterations are needed in the loop. For each iteration, you also check if the form was submitted and if the current field instance matches the name of the button used to submit the form. If so, you skip adding that field instance to the form (don't increment the instance counter this iteration to keep the total count synced).

The submit handlers for adding/removing fields simply set the 'rebuild' flag in the form state and return the fieldset to be updated (all instances get re-rendered each time). Adding/deleting field instances then become an indirect action of rebuilding the form with the correct number of field instances.

The examples module on drupal.org has an "add more" AJAX example which is fairly similar to what you want, except that it always removes the last field instance.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.