diff --git a/includes/form.inc b/includes/form.inc index 3d5f6f2..b407a5b 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -839,7 +839,9 @@ function drupal_process_form($form_id, &$form, &$form_state) { // cache when a form is processed, so scenarios that result in // the form being built behind the scenes and again for the // browser don't increment all the element IDs needlessly. - drupal_static_reset('drupal_html_id'); + if (!form_get_errors()) { + drupal_static_reset('drupal_html_id'); + } if ($form_state['submitted'] && !form_get_errors() && !$form_state['rebuild']) { // Execute form submit handlers. diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index 13fdca2..6cfb142 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -5,6 +5,35 @@ * Unit tests for the Drupal Form API. */ +/** + * Test duplicates elemens IDs when form validation fails. + */ +class FormsDuplicateElementIdsTestCase extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => 'Duplicates in DOM when form validation fails', + 'description' => 'Create two form on page and submit one of them.', + 'group' => 'Form API', + ); + } + + function setUp() { + parent::setUp('contact'); + } + + function testFormValidationFails() { + // Give anonymous user permission to use contact form. + user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form')); + // Go to page contains contact form. + $this->drupalGet('contact'); + // Submit contact form. + $this->drupalPost(NULL, array(), t('Send message')); + // Check duplicate IDs in DOM. + $this->assertNoDuplicateIds('There are no duplicate IDs'); + } +} + class FormsTestCase extends DrupalWebTestCase { public static function getInfo() {