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

Commit

Permalink
ajax page support
Browse files Browse the repository at this point in the history
  • Loading branch information
himiklab committed Jan 10, 2018
1 parent 3821957 commit bc9df57
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ For example:
or

```php
<?= \himiklab\yii2\recaptcha\ReCaptcha::widget([
'name' => 'reCaptcha',
'siteKey' => 'your siteKey',
'widgetOptions' => ['class' => 'col-sm-offset-3']
]) ?>
<?= $form->field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) ?>
```

or

```php
<?= $form->field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) ?>
<?= \himiklab\yii2\recaptcha\ReCaptcha::widget([
'name' => 'reCaptcha',
'siteKey' => 'your siteKey',
'widgetOptions' => ['class' => 'col-sm-offset-3']
]) ?>
```

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

Notes
-----
Exclude a reCaptcha field from ajax validation. It creates problem.

Resources
---------
* [Google reCAPTCHA](https://developers.google.com/recaptcha)
39 changes: 23 additions & 16 deletions ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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());
}
Expand Down

0 comments on commit bc9df57

Please sign in to comment.