From bc9df57a2b420aa7deaec4f6556ff79d57fa435b Mon Sep 17 00:00:00 2001 From: himiklab Date: Wed, 10 Jan 2018 14:21:29 +0300 Subject: [PATCH] ajax page support --- README.md | 16 ++++++---------- ReCaptcha.php | 39 +++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 05f9fcb..431a718 100644 --- a/README.md +++ b/README.md @@ -91,17 +91,17 @@ For example: or ```php - 'reCaptcha', - 'siteKey' => 'your siteKey', - 'widgetOptions' => ['class' => 'col-sm-offset-3'] -]) ?> +field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) ?> ``` or ```php -field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) ?> + 'reCaptcha', + 'siteKey' => 'your siteKey', + 'widgetOptions' => ['class' => 'col-sm-offset-3'] +]) ?> ``` or simply @@ -110,10 +110,6 @@ or simply 'reCaptcha']) ?> ``` -Notes ------ -Exclude a reCaptcha field from ajax validation. It creates problem. - Resources --------- * [Google reCAPTCHA](https://developers.google.com/recaptcha) diff --git a/ReCaptcha.php b/ReCaptcha.php index fac7e15..26b4ced 100644 --- a/ReCaptcha.php +++ b/ReCaptcha.php @@ -91,6 +91,7 @@ class ReCaptcha extends InputWidget public function run() { + $view = $this->view; if (empty($this->siteKey)) { /** @var ReCaptcha $reCaptcha */ $reCaptcha = Yii::$app->reCaptcha; @@ -102,12 +103,12 @@ public function run() } if (self::$firstWidget) { - $view = $this->view; $arguments = http_build_query([ 'hl' => $this->getLanguageSuffix(), 'render' => 'explicit', 'onload' => 'recaptchaOnloadCallback', ]); + $view->registerJsFile( self::JS_API_URL . '?' . $arguments, ['position' => $view::POS_END, 'async' => true, 'defer' => true] @@ -117,21 +118,23 @@ public function run() var recaptchaOnloadCallback = function() { jQuery(".g-recaptcha").each(function() { var reCaptcha = jQuery(this); - var recaptchaClientId = grecaptcha.render(reCaptcha.attr("id"), { - "callback": function(response) { - jQuery("#" + reCaptcha.attr("input-id")).val(response).trigger("change"); - if (reCaptcha.attr("data-callback")) { - eval("(" + reCaptcha.attr("data-callback") + ")(response)"); - } - }, - "expired-callback": function() { - jQuery("#" + reCaptcha.attr("input-id")).val(""); - if (reCaptcha.attr("data-expired-callback")) { - eval("(" + reCaptcha.attr("data-expired-callback") + ")()"); - } - }, - }); - reCaptcha.data("recaptcha-client-id", recaptchaClientId); + if (reCaptcha.data("recaptcha-client-id") == undefined) { + var recaptchaClientId = grecaptcha.render(reCaptcha.attr("id"), { + "callback": function(response) { + jQuery("#" + reCaptcha.attr("input-id")).val(response).trigger("change"); + if (reCaptcha.attr("data-callback")) { + eval("(" + reCaptcha.attr("data-callback") + ")(response)"); + } + }, + "expired-callback": function() { + jQuery("#" + reCaptcha.attr("input-id")).val(""); + if (reCaptcha.attr("data-expired-callback")) { + eval("(" + reCaptcha.attr("data-expired-callback") + ")()"); + } + }, + }); + reCaptcha.data("recaptcha-client-id", recaptchaClientId); + } }); }; JS @@ -140,6 +143,10 @@ public function run() self::$firstWidget = false; } + if (Yii::$app->request->isAjax) { + $view->registerJs('recaptchaOnloadCallback();', $view::POS_END); + } + $this->customFieldPrepare(); echo Html::tag('div', '', $this->buildDivOptions()); }