Skip to content

Commit

Permalink
Merge pull request #5 from passioneight/passioneight-patch-1
Browse files Browse the repository at this point in the history
fix: google recaptcha script not working properly for multiple forms on same page
  • Loading branch information
passioneight committed May 9, 2022
2 parents 91caece + 8cae480 commit 8e1bb2b
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/Resources/public/js/recaptcha.js
Expand Up @@ -17,34 +17,33 @@ googleRecaptchaScript.addEventListener('load', function() {
console.debug("Executing Google Recaptcha on action:", action);
}

var form = element.closest("form");

if(form) {
var submitButtons = form.querySelectorAll('[type="submit"]');
submitButtons = submitButtons ? submitButtons : [];

for (var submitButton of submitButtons) {
submitButton.addEventListener('click', function(e){
e.preventDefault();

grecaptcha.execute(_config.googleRecaptcha.publicKey, {action: action}).then(function (token) {
if (_config.googleRecaptcha.debug) {
console.debug("Received response for action:", action, token);
}

element.value = token;

var doSubmit = form.dispatchEvent(new Event('submit', {
cancelable: true
}));

if(doSubmit) {
form.submit();
}
});
var submitButtons = element.closest('form').querySelectorAll('[type="submit"]');
submitButtons = submitButtons ? submitButtons : [];

for (var submitButton of submitButtons) {
submitButton.addEventListener('click', function(e){
e.preventDefault();

var form = e.target.closest("form");

grecaptcha.execute(_config.googleRecaptcha.publicKey, {action: action}).then(function (token) {
if (_config.googleRecaptcha.debug) {
console.debug("Received response for action:", action, token);
}

form.querySelector(_config.googleRecaptcha.querySelector).value = token;

var doSubmit = form.dispatchEvent(new Event('submit', {
cancelable: true
}));

if(doSubmit) {
form.submit();
}
});
}
});
}

}
} else if (_config.googleRecaptcha.debug) {
console.debug("Could not find Google Recaptcha elements for query selector '" + _config.googleRecaptcha.querySelector + "'");
Expand Down

0 comments on commit 8e1bb2b

Please sign in to comment.