Does a form submit button field name and element name have to be unique?

by Kevin   Last Updated January 19, 2018 17:07 PM

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' => [
    '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?

Tags : 8 forms

Related Questions

Reuse form from another module in multistep form

Updated May 21, 2015 22:03 PM