Merge branch 'ticket1237' into 'develop'

usage de recaptcha V3 vanilla JS

See merge request service-web-lab/regalette!10
This commit is contained in:
Vincent BRIDIER 2022-12-06 12:12:59 +00:00
commit c32119d186
2 changed files with 37 additions and 27 deletions

View file

@ -184,7 +184,7 @@
(function(d, t) {
let g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.src = 'https://www.google.com/recaptcha/api.js?render=<?php esc_js(GOOGLE_RECAPTCHA_PUBLIC); ?>';
g.src = 'https://www.google.com/recaptcha/api.js?render=<?= esc_js(GOOGLE_RECAPTCHA_PUBLIC); ?>';
s.parentNode.insertBefore(g, s)
}(document, 'script'));
}

View file

@ -318,35 +318,45 @@ if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' &
</div>
</div>
<script>
let files = $("input[name=files]");
document.addEventListener("DOMContentLoaded", function() {
/** @type HTMLInputElement files */
const files = document.querySelector("input[name=files]");
if (files !== null) {
files.addEventListener('change', function(e) {
/** @type HTMLInputElement input */
const input = this;
const file = input.files[0];
if (input.files.length > 0) {
files.on('change', function(e) {
let file = $(this).prop('files')[0];
if ($(this).prop('files').length > 0) {
let filesize = ((file.size / 1024) / 1024).toFixed(4); // MB
let filesize = ((file.size / 1024) / 1024).toFixed(4); // MB
if (filesize > 2) {
alert("Poids de la pièce jointe supérieur à 2Mo.");
files.val('');
$(".contact-filename").html("Télécharger votre fichier...");
} else {
$(".contact-filename").html(file.name);
}
}
});
$("#contact-form").on('submit', function(e) {
e.preventDefault();
grecaptcha.ready(function() {
grecaptcha.execute('<?= esc_attr(GOOGLE_RECAPTCHA_PUBLIC); ?>', {
action: 'contact'
}).then(function(token) {
let recaptchaResponse = document.getElementById('recaptchaResponse');
recaptchaResponse.value = token;
$("#contact-form").unbind('submit').submit();
if (filesize > 2) {
alert("Poids de la pièce jointe supérieur à 2Mo.");
files.value = '';
document.querySelector(".contact-filename").innerHTML = "Télécharger votre fichier...";
} else {
document.querySelector(".contact-filename").innerHTML = file.name;
}
}
});
});
}
contactSubmitEventHandler = function(e) {
e.preventDefault();
grecaptcha.ready(function() {
grecaptcha.execute('<?= esc_attr(GOOGLE_RECAPTCHA_PUBLIC); ?>', {
action: 'contact'
}).then(function(token) {
const recaptchaResponse = document.getElementById('recaptchaResponse');
recaptchaResponse.value = token;
document.querySelector("#contact-form").removeEventListener('submit', contactSubmitEventHandler);
document.querySelector("#contact-form").submit();
});
});
};
document.querySelector("#contact-form").addEventListener('submit', contactSubmitEventHandler);
});
</script>