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

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
himiklab committed Nov 8, 2017
1 parent 037caa8 commit fedad36
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
15 changes: 7 additions & 8 deletions ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function buildDivOptions()
}

// The id attribute required for explicit reCaptcha initialization
$divOptions['id'] = $this->getReCaptchaId();
$divOptions['id'] = $this->getReCaptchaId() . '-recaptcha';

$divOptions += $this->widgetOptions;

Expand All @@ -161,7 +161,11 @@ protected function getReCaptchaId()
return $this->widgetOptions['id'];
}

return $this->id . '-recaptcha';
if ($this->hasModel()) {
return Html::getInputId($this->model, $this->attribute);
}

return $this->id . '-' . $this->name;
}

protected function getLanguageSuffix()
Expand Down Expand Up @@ -189,12 +193,7 @@ protected function customFieldPrepare()
$inputName = $this->name;
}

if (isset($this->widgetOptions['id'])) {
$inputId = $this->getReCaptchaId() . '-input';
} else {
$inputId = Html::getInputId($this->model, $this->attribute);
}

$inputId = $this->getReCaptchaId();
$verifyCallbackName = lcfirst(Inflector::id2camel($inputId)) . 'Callback';
$jsCode = $this->render('verify', [
'verifyCallbackName' => $verifyCallbackName,
Expand Down
5 changes: 4 additions & 1 deletion ReCaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yii;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\validators\Validator;

Expand Down Expand Up @@ -73,8 +74,10 @@ public function clientValidateAttribute($model, $attribute, $view)
'{attribute} cannot be blank.',
['attribute' => $model->getAttributeLabel($attribute)]
));
$recaptchaId = Html::getInputId($model, $attribute) . '-recaptcha';

return "(function(messages){if(!grecaptcha.getResponse()){messages.push('{$message}');}})(messages);";
return '(function(messages){if(!grecaptcha.getResponse(' .
"jQuery('#{$recaptchaId}').data('recaptcha-client-id'))){messages.push('{$message}');}})(messages);";
}

/**
Expand Down
3 changes: 2 additions & 1 deletion views/onload.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
var recaptchaOnloadCallback = function() {
jQuery(".g-recaptcha").each(function(index) {
var reCaptcha = jQuery(this);
grecaptcha.render(reCaptcha.attr("id"), {
var recaptchaClientId = grecaptcha.render(reCaptcha.attr("id"), {
'sitekey': reCaptcha.attr("data-sitekey"),
'callback': eval(reCaptcha.attr("data-callback")),
'theme': reCaptcha.attr("data-theme"),
'type': reCaptcha.attr("data-type"),
'size': reCaptcha.attr("data-size"),
'tabindex': reCaptcha.attr("data-tabindex")
});
reCaptcha.data('recaptcha-client-id', recaptchaClientId);
});
};
JS
Expand Down

0 comments on commit fedad36

Please sign in to comment.