Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Commit bc9df57

Browse files
committed
ajax page support
1 parent 3821957 commit bc9df57

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

Diff for: README.md

+6-10
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,17 @@ For example:
9191
or
9292

9393
```php
94-
<?= \himiklab\yii2\recaptcha\ReCaptcha::widget([
95-
'name' => 'reCaptcha',
96-
'siteKey' => 'your siteKey',
97-
'widgetOptions' => ['class' => 'col-sm-offset-3']
98-
]) ?>
94+
<?= $form->field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) ?>
9995
```
10096

10197
or
10298

10399
```php
104-
<?= $form->field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) ?>
100+
<?= \himiklab\yii2\recaptcha\ReCaptcha::widget([
101+
'name' => 'reCaptcha',
102+
'siteKey' => 'your siteKey',
103+
'widgetOptions' => ['class' => 'col-sm-offset-3']
104+
]) ?>
105105
```
106106

107107
or simply
@@ -110,10 +110,6 @@ or simply
110110
<?= \himiklab\yii2\recaptcha\ReCaptcha::widget(['name' => 'reCaptcha']) ?>
111111
```
112112

113-
Notes
114-
-----
115-
Exclude a reCaptcha field from ajax validation. It creates problem.
116-
117113
Resources
118114
---------
119115
* [Google reCAPTCHA](https://developers.google.com/recaptcha)

Diff for: ReCaptcha.php

+23-16
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class ReCaptcha extends InputWidget
9191

9292
public function run()
9393
{
94+
$view = $this->view;
9495
if (empty($this->siteKey)) {
9596
/** @var ReCaptcha $reCaptcha */
9697
$reCaptcha = Yii::$app->reCaptcha;
@@ -102,12 +103,12 @@ public function run()
102103
}
103104

104105
if (self::$firstWidget) {
105-
$view = $this->view;
106106
$arguments = http_build_query([
107107
'hl' => $this->getLanguageSuffix(),
108108
'render' => 'explicit',
109109
'onload' => 'recaptchaOnloadCallback',
110110
]);
111+
111112
$view->registerJsFile(
112113
self::JS_API_URL . '?' . $arguments,
113114
['position' => $view::POS_END, 'async' => true, 'defer' => true]
@@ -117,21 +118,23 @@ public function run()
117118
var recaptchaOnloadCallback = function() {
118119
jQuery(".g-recaptcha").each(function() {
119120
var reCaptcha = jQuery(this);
120-
var recaptchaClientId = grecaptcha.render(reCaptcha.attr("id"), {
121-
"callback": function(response) {
122-
jQuery("#" + reCaptcha.attr("input-id")).val(response).trigger("change");
123-
if (reCaptcha.attr("data-callback")) {
124-
eval("(" + reCaptcha.attr("data-callback") + ")(response)");
125-
}
126-
},
127-
"expired-callback": function() {
128-
jQuery("#" + reCaptcha.attr("input-id")).val("");
129-
if (reCaptcha.attr("data-expired-callback")) {
130-
eval("(" + reCaptcha.attr("data-expired-callback") + ")()");
131-
}
132-
},
133-
});
134-
reCaptcha.data("recaptcha-client-id", recaptchaClientId);
121+
if (reCaptcha.data("recaptcha-client-id") == undefined) {
122+
var recaptchaClientId = grecaptcha.render(reCaptcha.attr("id"), {
123+
"callback": function(response) {
124+
jQuery("#" + reCaptcha.attr("input-id")).val(response).trigger("change");
125+
if (reCaptcha.attr("data-callback")) {
126+
eval("(" + reCaptcha.attr("data-callback") + ")(response)");
127+
}
128+
},
129+
"expired-callback": function() {
130+
jQuery("#" + reCaptcha.attr("input-id")).val("");
131+
if (reCaptcha.attr("data-expired-callback")) {
132+
eval("(" + reCaptcha.attr("data-expired-callback") + ")()");
133+
}
134+
},
135+
});
136+
reCaptcha.data("recaptcha-client-id", recaptchaClientId);
137+
}
135138
});
136139
};
137140
JS
@@ -140,6 +143,10 @@ public function run()
140143
self::$firstWidget = false;
141144
}
142145

146+
if (Yii::$app->request->isAjax) {
147+
$view->registerJs('recaptchaOnloadCallback();', $view::POS_END);
148+
}
149+
143150
$this->customFieldPrepare();
144151
echo Html::tag('div', '', $this->buildDivOptions());
145152
}

0 commit comments

Comments
 (0)