Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor v3 twig to support multiple recaptcha on the same page, and … #295

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 60 additions & 15 deletions src/Resources/views/Form/v3/ewz_recaptcha_widget.html.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{% block ewz_recaptcha_widget %}
{% apply spaceless %}
{% if form.vars.ewz_recaptcha_enabled %}
<script src="{{ form.vars.ewz_recaptcha_api_uri }}?render={{ form.vars.public_key }}"></script>

{% if form.vars.ewz_recaptcha_hide_badge %}
<link rel="stylesheet" href="{{ asset('/bundles/ewzrecaptcha/css/recaptcha.css') }}">
{% endif %}
Expand All @@ -11,19 +9,66 @@
{{ 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 %}>
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;
HTMLFormElement.prototype.submit.call(grecaptchaForm);
});
});
}, false);
(function(){
var element = document.querySelector('#{{ id }}');
var form = element.form;

// recaptcha callback for this form

window['recaptchaOnloadCallback_{{ id }}_done'] = false;
window['recaptchaOnloadCallback_{{ id }}'] = function() {
addEventListener('submit', function(e) {
var element = document.querySelector('#{{ id }}');
var form = element.form;
if (e.target !== form) {
return
}
if (!window['recaptchaOnloadCallback_{{ id }}_done']) {
e.preventDefault(); // stop form submit
e.stopPropagation(); // stop also events
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) {
element.value = token;
window['recaptchaOnloadCallback_{{ id }}_done'] = true;
if (e.submitter) {
e.submitter.click(); // trigger also events
} else {
form.submit();
}
window['recaptchaOnloadCallback_{{ id }}_done'] = false;
});
}
}, true) // execute before other submit listeners
};

// recaptcha load for this form

var loadJS = function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '{{ form.vars.ewz_recaptcha_api_uri }}?render={{ form.vars.public_key }}&onload=recaptchaOnloadCallback_{{ id }}';
script.async = true;
document.body.appendChild(script);
};

// detect when form is visible

if (!('IntersectionObserver' in window) ||
!('IntersectionObserverEntry' in window) ||
!('intersectionRatio' in window.IntersectionObserverEntry.prototype)) {
loadJS();
} else {
var observer = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.intersectionRatio > 0) {
observer.disconnect();
loadJS();
}
});
}, { root: null });

observer.observe(form);
}
})();
</script>
{% endif %}
{% endapply %}
Expand Down