1

I have a single form that I am displaying on the same page twice in different areas. They each have a unique form id provided by getFormId().

However, only the first instance of the form works. The second instance doesn't submit via AJAX at all and doesn't adhere to validation.

I found that if I took out the first instance of the form, the second one works just fine.

My second step was to create a new form class, again with its own unique form ID. The second form still did not submit correctly.

I found out that I had to do a few things to get this to work, and now I have one form class again with the same form on the page twice:

$form['submit_' . $this->getFormId()] = [
  '#name' => 'submit_' . $this->getFormId(),
  '#type' => 'button',
  '#value' => $this->t('Sign up'),
  '#limit_validation_errors' => [],
  '#submit' => ['::submitForm'],
  '#attributes' => [
    'class' => [
      'hl__button',
      'hl__button--small',
      'hl__button--inline',
    ],
    'data-twig-suggestion' => 'newsletter_signup_submit',
  ],
  '#suffix' => '</div>',
  '#ajax' => [
    'wrapper' => $wrapper,
    'method' => 'replace',
    'callback' => '::validateEmailSubmission',
    'progress' => [
      '#type' => 'none',
    ],
  ],
];

I had to add both a unique #name and give the form item a unique name instead of just $form['submit']. When I did that, everything worked as expected.

Did I do something wrong here, or is this a potential core bug? Can you not have different forms with $form['submit'] as the submit name? I feel like I did this a lot in D7, but it does not work here at all. Even with two different form classes and unique form IDs, this problem persisted until I added the above code.

What would be the proper way to do submit buttons to avoid this?

  • Have you read through this post: drupal.org/forum/support/module-development-and-code-questions/… – bleen Jan 19 '18 at 16:37
  • Nope. Wish I had a day ago. Why can't core differentiate between this for AJAX forms, but can for normal forms? I must have completely missed this in the docs. – Kevin Jan 19 '18 at 16:47
  • I believe its because AJAX forms are dependent on the rendered output while all other forms have the benefit of using the FAPI array for all its processing. – bleen Jan 19 '18 at 16:49
  • For me normal form operations work in multiple ajax forms without this. Only ajax auto refocus tries to focus sometimes the wrong button of the first form. In my case only a minor glitch, I've switched off auto refocus, but renaming the submit button helps with refocus as well, so I think this is the proper way. – 4k4 Jan 19 '18 at 17:19
  • 1
    Okay, so the submit and/or action buttons should have unique names for AJAX forms. I did not know that, and probably what sent me around in circles the last few days. I also found this thread. drupal.org/project/drupal/issues/1342066 – Kevin Jan 19 '18 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.