-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInvisibleRecaptchaWidget.php
56 lines (50 loc) · 1.65 KB
/
InvisibleRecaptchaWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* InvisibleRecaptchaWidget class file.
* @copyright (c) 2018, Bariev Pavel
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace bariew\invisibleRecaptcha;
use yii\helpers\Html;
use yii\widgets\InputWidget;
/**
* @see README.md
* @author Pavel Bariev <bariew@yandex.ru>
*/
class InvisibleRecaptchaWidget extends InputWidget
{
/**
* @var string
*/
public $buttonText = 'Submit';
/**
* @inheritdoc
*/
public function run()
{
$this->getView()->registerJsFile('https://www.google.com/recaptcha/api.js', [
'async' => 'async',
'defer' => 'defer'
]);
$this->field->template = "{input}\n{error}";
$callbackRandomString = time();
$formId = $this->field->form->id;
$inputId = Html::getInputId($this->model, $this->attribute);
$recaptchaId = InvisibleRecaptchaValidator::POST_PARAM;
$options = array_merge([
'data-sitekey' => InvisibleRecaptchaValidator::key(),
'data-callback' => "recaptchaCallback_{$callbackRandomString}"
], $this->options, ['id' => 'recaptchaButton'.$callbackRandomString]);
Html::addCssClass($options, 'g-recaptcha recaptcha');
return Html::activeHiddenInput($this->model, $this->attribute)
. Html::button($this->buttonText, $options)
. Html::tag('script', <<<JS
var recaptchaCallback_{$callbackRandomString} = function() {
$('#{$inputId}').val($('#{$recaptchaId}').val());
$('#{$formId}').submit();
}
grecaptcha.render('recaptchaButton{$callbackRandomString}'); // this helps to ajax form refresh
JS
);
}
}