Skip to content

Commit 30fe495

Browse files
muratcakirMichael-Herzog
authored andcommitted
CAPTCHA: Submit form via requestSubmit vs submit to transfer submit button's name/value pair
(cherry picked from commit b7c05d5)
1 parent fae7be9 commit 30fe495

1 file changed

Lines changed: 31 additions & 50 deletions

File tree

src/Smartstore.Web/wwwroot/js/smartstore.captcha.js

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -106,69 +106,50 @@
106106
form.__captchaSubmitButton = button;
107107
},
108108

109-
/**
110-
* Create a temporary hidden input for the submit button's name/value
111-
* @param {HTMLFormElement} form
112-
* @returns {HTMLInputElement|null} The created input (to be cleaned up later)
113-
* @private
114-
*/
115-
_createButtonInput: function (form) {
116-
if (!form || !form.__captchaSubmitButton) return null;
117-
118-
var button = form.__captchaSubmitButton;
119-
if (!button.name || button.disabled) return null;
120-
121-
// Create a temporary hidden input with the button's name/value
122-
var input = document.createElement('input');
123-
input.type = 'hidden';
124-
input.name = button.name;
125-
input.value = button.value || '';
126-
input.setAttribute('data-captcha-button-input', 'true');
127-
form.appendChild(input);
128-
129-
return input;
130-
},
131-
132-
/**
133-
* Remove temporary button input after submit
134-
* @param {HTMLFormElement} form
135-
* @private
136-
*/
137-
_cleanupButtonInput: function (form) {
138-
if (!form) return;
139-
var input = form.querySelector('input[data-captcha-button-input="true"]');
140-
if (input) {
141-
try { input.remove(); } catch (_) { /* IE11 fallback */ form.removeChild(input); }
142-
}
143-
},
144-
145109
/**
146110
* Resubmit form honoring jQuery Unobtrusive AJAX and preserving submit button context
111+
* Uses modern requestSubmit() when available, falls back to trigger('submit') with hidden input
147112
* @param {HTMLFormElement} form
148113
* @param {string} reentryGuardName - Guard property name (e.g., '__captchaResubmit')
149114
*/
150115
resubmitForm: function (form, reentryGuardName) {
151116
if (!form) return;
152117
const guard = reentryGuardName || '__captchaResubmit';
153-
const isUnobtrusive = this.isUnobtrusiveAjax(form);
118+
const button = form.__captchaSubmitButton;
154119

155-
// Create temporary hidden input for the submit button (if present)
156-
var buttonInput = this._createButtonInput(form);
120+
form[guard] = true;
121+
try {
122+
// Modern: Use requestSubmit (clean, native, includes button automatically)
123+
if (typeof form.requestSubmit === 'function') {
124+
form.requestSubmit(button || null);
125+
}
126+
// Fallback: jQuery Unobtrusive + temporary hidden input for legacy browsers
127+
else if (this.isUnobtrusiveAjax(form) && window.jQuery) {
128+
// Create temporary input for button name/value
129+
var tempInput = null;
130+
if (button && button.name && !button.disabled) {
131+
tempInput = document.createElement('input');
132+
tempInput.type = 'hidden';
133+
tempInput.name = button.name;
134+
tempInput.value = button.value || '';
135+
form.appendChild(tempInput);
136+
}
157137

158-
if (isUnobtrusive && window.jQuery) {
159-
form[guard] = true;
160-
try {
161138
window.jQuery(form).trigger('submit');
139+
140+
// Cleanup after serialization (100ms delay to let unobtrusive serialize first)
141+
if (tempInput) {
142+
setTimeout(function () {
143+
try { tempInput.remove(); } catch (_) { form.removeChild(tempInput); }
144+
}, 100);
145+
}
162146
}
163-
finally {
164-
form[guard] = false;
165-
// Cleanup after a short delay (let unobtrusive serialize the form first)
166-
setTimeout(function () { this._cleanupButtonInput(form); }.bind(this), 100);
147+
// Last resort: native submit (no button context, but works)
148+
else {
149+
form.submit();
167150
}
168-
} else {
169-
form.submit();
170-
// Cleanup immediately for non-AJAX submits (form will navigate away anyway)
171-
this._cleanupButtonInput(form);
151+
} finally {
152+
form[guard] = false;
172153
}
173154
},
174155

0 commit comments

Comments
 (0)