Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Commit b8087d7

Browse files
Execute the V3 Challenge on form submit, not on page load
1 parent c4dcc71 commit b8087d7

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

src/Resources/views/Form/v3/ewz_recaptcha_widget.html.twig

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@
77
<link rel="stylesheet" href="{{ asset('/bundles/ewzrecaptcha/css/recaptcha.css') }}">
88
{% endif %}
99

10+
{{ form_label(form) }}
11+
{{ form_widget(form) }}
12+
1013
<script{% if form.vars.script_nonce_csp is defined and form.vars.script_nonce_csp is not same as('') %} nonce="{{ form.vars.script_nonce_csp }}"{% endif %}>
11-
grecaptcha.ready(function () {
12-
grecaptcha.execute('{{ form.vars.public_key }}', { action: '{{ form.vars.action_name|default(constant('EWZ\\Bundle\\RecaptchaBundle\\Form\\Type\\EWZRecaptchaV3Type::DEFAULT_ACTION_NAME')) }}' }).then(function (token) {
13-
var recaptchaResponse = document.getElementById('{{ id }}');
14-
recaptchaResponse.value = token;
14+
var grecaptchaInput = document.getElementById('{{ id }}');
15+
grecaptchaInput.value = ''; // Always reset the value to get a brand new challenge
16+
var grecaptchaForm = grecaptchaInput.form;
17+
grecaptchaForm.addEventListener('submit', function (e) {
18+
e.preventDefault();
19+
20+
grecaptcha.ready(function () {
21+
grecaptcha.execute('{{ form.vars.public_key }}', { action: '{{ form.vars.action_name|default(constant('EWZ\\Bundle\\RecaptchaBundle\\Form\\Type\\EWZRecaptchaV3Type::DEFAULT_ACTION_NAME')) }}' }).then(function (token) {
22+
grecaptchaInput.value = token;
23+
grecaptchaForm.submit();
24+
});
1525
});
16-
});
26+
}, false);
1727
</script>
18-
19-
{{ form_label(form) }}
20-
{{ form_widget(form) }}
2128
{% endif %}
2229
{% endapply %}
2330
{% endblock %}

0 commit comments

Comments
 (0)