@@ -91,6 +91,7 @@ class ReCaptcha extends InputWidget
91
91
92
92
public function run ()
93
93
{
94
+ $ view = $ this ->view ;
94
95
if (empty ($ this ->siteKey )) {
95
96
/** @var ReCaptcha $reCaptcha */
96
97
$ reCaptcha = Yii::$ app ->reCaptcha ;
@@ -102,12 +103,12 @@ public function run()
102
103
}
103
104
104
105
if (self ::$ firstWidget ) {
105
- $ view = $ this ->view ;
106
106
$ arguments = http_build_query ([
107
107
'hl ' => $ this ->getLanguageSuffix (),
108
108
'render ' => 'explicit ' ,
109
109
'onload ' => 'recaptchaOnloadCallback ' ,
110
110
]);
111
+
111
112
$ view ->registerJsFile (
112
113
self ::JS_API_URL . '? ' . $ arguments ,
113
114
['position ' => $ view ::POS_END , 'async ' => true , 'defer ' => true ]
@@ -117,21 +118,23 @@ public function run()
117
118
var recaptchaOnloadCallback = function() {
118
119
jQuery(".g-recaptcha").each(function() {
119
120
var reCaptcha = jQuery(this);
120
- var recaptchaClientId = grecaptcha.render(reCaptcha.attr("id"), {
121
- "callback": function(response) {
122
- jQuery("#" + reCaptcha.attr("input-id")).val(response).trigger("change");
123
- if (reCaptcha.attr("data-callback")) {
124
- eval("(" + reCaptcha.attr("data-callback") + ")(response)");
125
- }
126
- },
127
- "expired-callback": function() {
128
- jQuery("#" + reCaptcha.attr("input-id")).val("");
129
- if (reCaptcha.attr("data-expired-callback")) {
130
- eval("(" + reCaptcha.attr("data-expired-callback") + ")()");
131
- }
132
- },
133
- });
134
- reCaptcha.data("recaptcha-client-id", recaptchaClientId);
121
+ if (reCaptcha.data("recaptcha-client-id") == undefined) {
122
+ var recaptchaClientId = grecaptcha.render(reCaptcha.attr("id"), {
123
+ "callback": function(response) {
124
+ jQuery("#" + reCaptcha.attr("input-id")).val(response).trigger("change");
125
+ if (reCaptcha.attr("data-callback")) {
126
+ eval("(" + reCaptcha.attr("data-callback") + ")(response)");
127
+ }
128
+ },
129
+ "expired-callback": function() {
130
+ jQuery("#" + reCaptcha.attr("input-id")).val("");
131
+ if (reCaptcha.attr("data-expired-callback")) {
132
+ eval("(" + reCaptcha.attr("data-expired-callback") + ")()");
133
+ }
134
+ },
135
+ });
136
+ reCaptcha.data("recaptcha-client-id", recaptchaClientId);
137
+ }
135
138
});
136
139
};
137
140
JS
@@ -140,6 +143,10 @@ public function run()
140
143
self ::$ firstWidget = false ;
141
144
}
142
145
146
+ if (Yii::$ app ->request ->isAjax ) {
147
+ $ view ->registerJs ('recaptchaOnloadCallback(); ' , $ view ::POS_END );
148
+ }
149
+
143
150
$ this ->customFieldPrepare ();
144
151
echo Html::tag ('div ' , '' , $ this ->buildDivOptions ());
145
152
}
0 commit comments