1313use yii \base \InvalidConfigException ;
1414use yii \di \Instance ;
1515use yii \helpers \Html ;
16+ use yii \web \View ;
1617use yii \widgets \InputWidget ;
1718
1819class 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+ });
8788JS
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