Skip to content

Commit 100c515

Browse files
committed
Issue [#3580681] by smulvih2 - Edge case with webform Previous button
1 parent a4c6614 commit 100c515

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

modules/custom/wxt_ext/wxt_ext_webform/js/wet-webform-prev-bypass.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
(function (Drupal, once) {
22
Drupal.behaviors.wetWebformPrevBypass = {
3-
attach: function (context) {
3+
attach(context) {
4+
45
// Find Previous button with attribute.
5-
var elements = once('wetWebformPrevBypass', '[data-wet-skip-validation]', context);
6+
const elements = once(
7+
'wetWebformPrevBypass',
8+
'[data-wet-skip-validation]',
9+
context
10+
);
611

7-
elements.forEach(function (el) {
8-
// Runs BEFORE jQuery/WET handlers.
12+
elements.forEach((el) => {
913
el.addEventListener('click', function (e) {
10-
var form = el.form;
14+
const form = el.form;
1115
if (!form) {
1216
return;
1317
}
@@ -17,8 +21,23 @@
1721
e.stopImmediatePropagation();
1822
e.stopPropagation();
1923

20-
// Native submit: does NOT trigger jQuery submit handlers,
21-
// so no WET validation, but Drupal still processes "op".
24+
// Remove any earlier injected helper input.
25+
const existing = form.querySelector('input[data-wet-prev-helper="1"]');
26+
if (existing) {
27+
existing.remove();
28+
}
29+
30+
// Preserve the clicked submit button, because form.submit()
31+
// does not include submit button name/value automatically.
32+
if (el.name) {
33+
const hidden = document.createElement('input');
34+
hidden.type = 'hidden';
35+
hidden.name = el.name;
36+
hidden.value = el.value;
37+
hidden.setAttribute('data-wet-prev-helper', '1');
38+
form.appendChild(hidden);
39+
}
40+
2241
form.submit();
2342
}, true);
2443
});

0 commit comments

Comments
 (0)