Hello,

I stumbled upon this bug on a Drupal 6 website, but was able to reproduce it on a clean Drupal 7.

I have a form, which callback takes an argument, telling if it should display or not "advanced fields". On some pages, I display standard and advanced form (I use quicktabs to switch from one to another, but this is irrelevant). No problem with this, each instance of the form gets a different HTML id (the second one's gets '-1' appended).
If I submit one of these forms with a case causing form validation error (throwing form_set_error), in the resulting page, the two instances of my form get the same HTML id (none of them get a '-1' appendend). While debugging a little, I found that a call to form_clean_id(NULL, TRUE) is made between the rendering of the two form's instances, thus flushinf its static variable, and ignoring any anterior duplicate id.

I attached a D7 test module showing the bug : just fill in less than 3 letters in the text field, submit, and check the HTML id of the two <form> tags : they are the same. It does not happen if there is not validation error.

Here is the module's code :


function custom_menu() {
  $items = array();
  $items['custom'] = array(
    'page callback' => 'custom_page',
    'access callback' => TRUE,
  );
  return $items;
}

function custom_page() {
  $output = array();
  $output[] = 'Normal form:';
  $output[] = drupal_get_form('custom_form');
  $output[] = 'Advanced form:';
  $output[] = drupal_get_form('custom_form', TRUE);
  return $output;
}

function custom_form($form_state, $advanced = FALSE) {
  $form['text'] = array(
    '#type' => 'textfield',
    '#title' => 'Text',
  );

  if ($advanced) {
    $form['advanced'] = array(
      '#type' => 'select',
      '#title' => 'Choose an option',
      '#options' => array('No', 'Yes'),
    );
  }

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'OK',
  );

  return $form;
}

function custom_form_validate($form, $form_state) {
  if (strlen($form_state['values']['text']) < 3) {
    form_set_error('text', 'Type at least 3 characters.');
  }
}

function custom_form_submit($form, $form_state) {
  drupal_set_message('You typed: '. $form_state['values']['text']);
}

Regards,
David

CommentFileSizeAuthor
custom.tar_.gz680 bytesDavid Stosik

Comments

bleen’s picture

Status: Active » Closed (duplicate)