Skip to content

Commit cb8ee60

Browse files
committed
Issue victorjonsson#512, Multiple captchas attempted fix
In addition to previous commit, I realized that the Google validation callback needed to identify which form so it wouldn't mark both forms, so I created an array of callback functions each with a reference to a form ID (which is created if not already present). Fingers crossed this won't blow anything up! For @jeesus to test with.
1 parent 7ced80d commit cb8ee60

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/modules/security.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,12 @@
553553
$forms = $('form');
554554
}
555555

556+
var i=0,
557+
grecaptchaRenderCallback = [];
556558
$forms.each(function () {
557559
var $form = $(this),
558560
config = $form.context.validationConfig || false;
559-
if (config) {
561+
if (config) {
560562

561563
$('[data-validation~="recaptcha"]', $form).each(function () {
562564
var $input = $(this),
@@ -570,21 +572,26 @@
570572
throw new Error('Google reCaptcha site key is required.');
571573
}
572574

573-
var grecaptchaRenderCallback = function (result) {
574-
$('form').each(function () {
575+
if (!$form.attr('id')) {
576+
$form.attr('id', 'recaptcha-form-' + (i++));
577+
};
578+
grecaptchaRenderCallback[$form.attr('id')] = function (result) {
579+
var formID;
580+
$('#' + formID).each(function () {
575581
$('[data-validation~="recaptcha"]', $(this)).each(function () {
576582
$(this).trigger('validation', (result && result !== ''));
577583
});
578584
});
579585
};
586+
grecaptchaRenderCallback[$form.attr('id')].formID = $form.attr('id');
580587

581588
var widgetId = grecaptcha.render(div, {
582589
sitekey: siteKey,
583590
theme: theme,
584591
size: size,
585592
type: type,
586-
callback: grecaptchaRenderCallback,
587-
'expired-callback': grecaptchaRenderCallback
593+
callback: grecaptchaRenderCallback[$form.attr('id')],
594+
'expired-callback': grecaptchaRenderCallback[$form.attr('id')]
588595
});
589596

590597
$input

0 commit comments

Comments
 (0)