Skip to content

Commit 9717587

Browse files
Bahaa OdehBahaa Odeh
authored andcommitted
create widget
1 parent 9bd2e29 commit 9717587

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

RecaptchaV3Widget.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use yii\base\InvalidConfigException;
1414
use yii\di\Instance;
1515
use yii\helpers\Html;
16+
use yii\web\View;
1617
use yii\widgets\InputWidget;
1718

1819
class RecaptchaV3Widget extends InputWidget
@@ -53,6 +54,7 @@ public function init()
5354
}
5455

5556

57+
5658
/**
5759
* @inheritdoc
5860
*/
@@ -65,28 +67,28 @@ public function run()
6567
$callbackRandomString = time();
6668

6769
$options = array_merge([
68-
'onClick' => "recaptchaCallback_{$callbackRandomString}()"
70+
// 'onClick' => "recaptchaCallback_{$callbackRandomString}()"
6971
], $this->options);
7072

7173

72-
return
73-
Html::tag('script', <<<JS
74-
var recaptchaCallback_{$callbackRandomString} = function() {
75-
if(!$('#{$inputId}').val()){
76-
grecaptcha.ready(function() {
77-
grecaptcha.execute('{$this->_component->site_key}', {action: '{$this->actionName}'}).then(function(token) {
78-
alert(token);
79-
$('#{$inputId}').val(token);
80-
$('#{$formId}').submit();
81-
});
82-
});
83-
}else{
84-
$('#{$formId}').submit();
85-
}
86-
}
74+
$this->view->registerJs(<<<JS
75+
$('#{$formId}').on('beforeSubmit',function(){
76+
if(!$('#{$inputId}').val()){
77+
grecaptcha.ready(function() {
78+
grecaptcha.execute('{$this->_component->site_key}', {action: '{$this->actionName}'}).then(function(token) {
79+
$('#{$inputId}').val(token);
80+
$('#{$formId}').submit();
81+
});
82+
});
83+
return true;
84+
}else{
85+
return false;
86+
}
87+
});
8788
JS
88-
)
89-
. Html::activeHiddenInput($this->model, $this->attribute)
90-
. Html::button($this->buttonText, $options);
89+
,View::POS_READY);
90+
return
91+
92+
Html::activeHiddenInput($this->model, $this->attribute,['value'=>'']);
9193
}
9294
}

0 commit comments

Comments
 (0)