Skip to content

Commit

Permalink
Merge pull request #286 from damienalexandre/patch-1
Browse files Browse the repository at this point in the history
Execute the V3 Challenge on form submit, not on page load
  • Loading branch information
excelwebzone authored Apr 8, 2022
2 parents 6c59c67 + b8087d7 commit e079503
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/Resources/views/Form/v3/ewz_recaptcha_widget.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
<link rel="stylesheet" href="{{ asset('/bundles/ewzrecaptcha/css/recaptcha.css') }}">
{% endif %}

{{ form_label(form) }}
{{ form_widget(form) }}

<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 %}>
grecaptcha.ready(function () {
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) {
var recaptchaResponse = document.getElementById('{{ id }}');
recaptchaResponse.value = token;
var grecaptchaInput = document.getElementById('{{ id }}');
grecaptchaInput.value = ''; // Always reset the value to get a brand new challenge
var grecaptchaForm = grecaptchaInput.form;
grecaptchaForm.addEventListener('submit', function (e) {
e.preventDefault();
grecaptcha.ready(function () {
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) {
grecaptchaInput.value = token;
grecaptchaForm.submit();
});
});
});
}, false);
</script>

{{ form_label(form) }}
{{ form_widget(form) }}
{% endif %}
{% endapply %}
{% endblock %}

0 comments on commit e079503

Please sign in to comment.