|
530 | 530 | return this;
|
531 | 531 | };
|
532 | 532 |
|
533 |
| - var setupGooglereCaptcha = function (evt, $forms, config) |
534 |
| - { |
535 |
| - if( typeof grecaptcha !== typeof undefined && !$.formUtils.hasLoadedGrecaptcha ){ |
536 |
| - throw new Error('reCaptcha API can not be loaded by hand, delete reCaptcha API snippet.'); |
537 |
| - } else if(!$.formUtils.hasLoadedGrecaptcha) |
538 |
| - { |
539 |
| - $.formUtils.hasLoadedGrecaptcha = true; |
540 |
| - |
541 |
| - var src = '//www.google.com/recaptcha/api.js?onload=reCaptchaLoaded&render=explicit' + (config.lang ? '&hl=' + config.lang : ''); |
542 |
| - var script = document.createElement('script'); |
543 |
| - script.type = 'text/javascript'; |
544 |
| - script.async = true; |
545 |
| - script.defer = true; |
546 |
| - script.src = src; |
547 |
| - document.getElementsByTagName('body')[0].appendChild(script); |
548 |
| - } |
549 |
| - }; |
550 |
| - |
551 |
| - window.reCaptchaLoaded = function () |
552 |
| - { |
553 |
| - var $forms = $('form'); |
554 |
| - |
555 |
| - if (!$forms.each) |
556 |
| - { |
557 |
| - $forms = $($forms); |
| 533 | + var setupGooglereCaptcha = function (evt, $forms, config) { |
| 534 | + if (typeof grecaptcha !== typeof undefined && !$.formUtils.hasLoadedGrecaptcha) { |
| 535 | + throw new Error('reCaptcha API can not be loaded by hand, delete reCaptcha API snippet.'); |
| 536 | + } else if (!$.formUtils.hasLoadedGrecaptcha) { |
| 537 | + $.formUtils.hasLoadedGrecaptcha = true; |
| 538 | + |
| 539 | + var src = '//www.google.com/recaptcha/api.js?onload=reCaptchaLoaded&render=explicit' + (config.lang ? '&hl=' + config.lang : ''); |
| 540 | + var script = document.createElement('script'); |
| 541 | + script.type = 'text/javascript'; |
| 542 | + script.async = true; |
| 543 | + script.defer = true; |
| 544 | + script.src = src; |
| 545 | + document.getElementsByTagName('body')[0].appendChild(script); |
| 546 | + } |
| 547 | + }; |
| 548 | + |
| 549 | + window.reCaptchaLoaded = function ($forms) { |
| 550 | + if (!$forms || typeof $forms) { |
| 551 | + $forms = $('form'); |
| 552 | + } |
| 553 | + |
| 554 | + $forms.each(function () { |
| 555 | + var $form = $(this), |
| 556 | + config = $form.context.validationConfig; |
| 557 | + |
| 558 | + $('[data-validation~="recaptcha"]', $form).each(function () { |
| 559 | + var $input = $(this), |
| 560 | + div = document.createElement('DIV'), |
| 561 | + siteKey = config.reCaptchaSiteKey || $input.valAttr('recaptcha-sitekey'), |
| 562 | + theme = config.reCaptchaTheme || $input.valAttr('recaptcha-theme') || 'light'; |
| 563 | + |
| 564 | + if (!siteKey) { |
| 565 | + throw new Error('Google reCaptcha site key is required.'); |
558 | 566 | }
|
559 | 567 |
|
560 |
| - $forms.each(function () |
561 |
| - { |
562 |
| - var $form = $(this), |
563 |
| - config = $form.context.validationConfig; |
564 |
| - |
565 |
| - $('[data-validation~="recaptcha"]', $form).each(function () |
566 |
| - { |
567 |
| - var $el = $(this), |
568 |
| - div = document.createElement('DIV'), |
569 |
| - siteKey = config.reCaptchaSiteKey || $el.valAttr('recaptcha-sitekey'), |
570 |
| - theme = config.reCaptchaTheme || $el.valAttr('recaptcha-theme') || 'light'; |
571 |
| - |
572 |
| - if( !siteKey ){ |
573 |
| - throw new Error('Google reCaptcha site key is required.'); |
574 |
| - } |
575 |
| - |
576 |
| - $el.hide(); |
577 |
| - $el.parent().append(div); |
| 568 | + var widgetId = grecaptcha.render(div, { |
| 569 | + sitekey: siteKey, |
| 570 | + theme: theme |
| 571 | + }); |
578 | 572 |
|
579 |
| - var widgetId = grecaptcha.render(div, { |
580 |
| - sitekey: siteKey, |
581 |
| - theme: theme |
582 |
| - }); |
| 573 | + $input |
| 574 | + .valAttr('recaptcha-widget-id', widgetId) |
| 575 | + .hide() |
| 576 | + .on('beforeValidation', function(evt) { |
| 577 | + // prevent validator from skipping this input becaus its hidden |
| 578 | + evt.stopImmediatePropagation(); |
| 579 | + }) |
| 580 | + .parent() |
| 581 | + .append(div); |
583 | 582 |
|
584 |
| - $el |
585 |
| - .valAttr('recaptcha-widgetId', widgetId); |
586 |
| - }); |
| 583 | + }); |
587 | 584 |
|
588 |
| - }); |
589 |
| - }; |
| 585 | + }); |
| 586 | + }; |
590 | 587 |
|
591 |
| - $(window).on('validatorsLoaded formValidationSetup', setupGooglereCaptcha); |
| 588 | + $(window).on('validatorsLoaded formValidationSetup', setupGooglereCaptcha); |
592 | 589 |
|
593 | 590 | })(jQuery, window);
|
0 commit comments