AJAX fails with multiple instances of one webform

Created on 3 April 2018, about 7 years ago
Updated 1 May 2025, about 1 month ago

Problem/Motivation

I render a list of node teasers on a page, each of them has a webform attached (same kind of webform, passing in nid to a hidden field). Submission is handled by AJAX. When I submit only one, it's fine. When a second one is submitted, it doesn't pick up the wrapper id.

You can reproduce this simply by attaching multiple instances of a webform to a node display like this:

[theme]_preprocess_node(&$vars) {
  $vars['form_one'] = [
    '#type' => 'webform',
    '#webform' => '[webform]',
    '#default_data' => ['nid' => 1]
  ];

  $vars['form_two'] = [
    '#type' => 'webform',
    '#webform' => '[webform]',
    '#default_data' => ['nid' => 2]
  ];
}

Proposed resolution

My solution to resolve this temporarily was to amend WebformAjaxFormTrait::getWrapperId to include the passed through 'nid' parameter in the wrapper id. That's obviously not the way this can be resolved, but I guess it's the place where the fix could be made (works fine for me after the change).
(Description of the proposed solution, the rationale behind it, and workarounds for people who cannot use the patch.)

πŸ› Bug report
Status

Fixed

Version

6.0

Component

Code

Created by

πŸ‡¨πŸ‡ΏCzech Republic mvantuch

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States chrisolof

    I just ran into this issue with webform 6.2.x. Two instances of the same AJAX webform on the page - both tied to the page's node as source entity. Both end up on the page with the same HTML ID in their form wrappers.

    If I interact with the second webform instance (change to the next form page, for instance) nothing happens, but the first webform instance is driven instead. Basically we've got cross-talk on the AJAX channel between these two forms. Odd that they both come out with the same HTML ID. I hope to post a patch soon.

  • πŸ‡ΊπŸ‡ΈUnited States chrisolof

    No luck with the patch. I swapped out Html::getId() for Html::getUniqueId(), which gave me a guaranteed page-unique wrapper ID. I then attempted to persist this unique wrapper ID in the form state for later use in subsequent AJAX requests. Unfortunately it seems that the initial form state is lost upon the first AJAX request, which meant the original wrapper ID was lost too (and the AJAX connection to that specific instance of the form with it).

    It's possible someone more familiar with webform's AJAX system may be able to sort that out and somehow hang onto the wrapper ID. Maybe it's a bug that the initial form state is lost like that...

  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    You should probably open a new issue, this one is closed with no way to reopen except for the maintainer.

Production build 0.71.5 2024