|
558 | 558 | $forms = $('form'); |
559 | 559 | } |
560 | 560 |
|
561 | | - var i=0, |
562 | | - grecaptchaRenderCallback = []; |
563 | 561 | $forms.each(function () { |
564 | 562 | var $form = $(this), |
565 | 563 | 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'; |
575 | 564 |
|
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 | + } |
579 | 577 |
|
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 | + } |
589 | 592 | }); |
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); |
600 | 602 | }); |
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 | + } |
615 | 604 | }); |
616 | 605 | }; |
617 | 606 |
|
|
0 commit comments