Skip to content

Commit 5c01dad

Browse files
committed
Fixed bug in recaptcha victorjonsson#605
1 parent b2eaeda commit 5c01dad

File tree

1 file changed

+36
-47
lines changed

1 file changed

+36
-47
lines changed

src/modules/security.js

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -558,60 +558,49 @@
558558
$forms = $('form');
559559
}
560560

561-
var i=0,
562-
grecaptchaRenderCallback = [];
563561
$forms.each(function () {
564562
var $form = $(this),
565563
config = $form.context.validationConfig || false;
566-
if (config) {
567-
568-
$('[data-validation~="recaptcha"]', $form).each(function () {
569-
var $input = $(this),
570-
div = document.createElement('DIV'),
571-
siteKey = config.reCaptchaSiteKey || $input.valAttr('recaptcha-sitekey'),
572-
theme = config.reCaptchaTheme || $input.valAttr('recaptcha-theme') || 'light',
573-
size = config.reCaptchaSize || $input.valAttr('recaptcha-size') || 'normal',
574-
type = config.reCaptchaType || $input.valAttr('recaptcha-type') || 'image';
575564

576-
if (!siteKey) {
577-
throw new Error('Google reCaptcha site key is required.');
578-
}
565+
if (config) {
566+
$('[data-validation~="recaptcha"]', $form).each(function () {
567+
var $input = $(this),
568+
div = document.createElement('DIV'),
569+
siteKey = config.reCaptchaSiteKey || $input.valAttr('recaptcha-sitekey'),
570+
theme = config.reCaptchaTheme || $input.valAttr('recaptcha-theme') || 'light',
571+
size = config.reCaptchaSize || $input.valAttr('recaptcha-size') || 'normal',
572+
type = config.reCaptchaType || $input.valAttr('recaptcha-type') || 'image';
573+
574+
if (!siteKey) {
575+
throw new Error('Google reCaptcha site key is required.');
576+
}
579577

580-
if (!$form.attr('id')) {
581-
$form.attr('id', 'recaptcha-form-' + (i++));
582-
}
583-
grecaptchaRenderCallback[$form.attr('id')] = function (result) {
584-
var formID;
585-
$('#' + formID).each(function () {
586-
$('[data-validation~="recaptcha"]', $(this)).each(function () {
587-
$(this).trigger('validation', (result && result !== ''));
588-
});
578+
var widgetId = grecaptcha.render(div, {
579+
sitekey: siteKey,
580+
theme: theme,
581+
size: size,
582+
type: type,
583+
callback: function (result) {
584+
$form.find('[data-validation~="recaptcha"]').each(function () {
585+
$(this).trigger('validation', (result && result !== ''));
586+
});
587+
},
588+
'expired-callback': function() {
589+
console.log('redo....');
590+
$form.find('[data-validation~="recaptcha"]').trigger('validation', false);
591+
}
589592
});
590-
};
591-
grecaptchaRenderCallback[$form.attr('id')].formID = $form.attr('id');
592-
593-
var widgetId = grecaptcha.render(div, {
594-
sitekey: siteKey,
595-
theme: theme,
596-
size: size,
597-
type: type,
598-
callback: grecaptchaRenderCallback[$form.attr('id')],
599-
'expired-callback': grecaptchaRenderCallback[$form.attr('id')]
593+
$input
594+
.valAttr('recaptcha-widget-id', widgetId)
595+
.hide()
596+
.on('beforeValidation', function (evt) {
597+
// prevent validator from skipping this input because its hidden
598+
evt.stopImmediatePropagation();
599+
})
600+
.parent()
601+
.append(div);
600602
});
601-
602-
$input
603-
.valAttr('recaptcha-widget-id', widgetId)
604-
.hide()
605-
.on('beforeValidation', function (evt) {
606-
// prevent validator from skipping this input becaus its hidden
607-
evt.stopImmediatePropagation();
608-
})
609-
.parent()
610-
.append(div);
611-
612-
});
613-
}
614-
603+
}
615604
});
616605
};
617606

0 commit comments

Comments
 (0)