fix(public): réinitialisation dynamique et nettoyage serveur des champs du formulaire selon le type de demande
This commit is contained in:
+14
@@ -135,6 +135,20 @@ public class PublicInscriptionController {
|
||||
return "redirect:/inscription-public/demarrer";
|
||||
}
|
||||
|
||||
// Nettoyage défensif des champs non applicables
|
||||
if ("RENOUVELLEMENT".equals(preInscription.getTypeDemande())) {
|
||||
preInscription.setAncienClub(null);
|
||||
preInscription.setRaisonChangementClub(null);
|
||||
preInscription.setAncienneCategorie(null);
|
||||
preInscription.setCommentConnuClub(null);
|
||||
}
|
||||
if (preInscription.getDateNaissance() != null) {
|
||||
int age = java.time.Period.between(preInscription.getDateNaissance(), java.time.LocalDate.now()).getYears();
|
||||
if (age >= 18) {
|
||||
preInscription.setRepresentantLegal(null);
|
||||
}
|
||||
}
|
||||
|
||||
// Sauvegarde de la pré-inscription
|
||||
preInscription.setSaison(activeSaison);
|
||||
preInscription.setStatutTraite(false);
|
||||
|
||||
@@ -206,6 +206,7 @@
|
||||
|
||||
<script>
|
||||
const form = document.querySelector('form');
|
||||
|
||||
form.addEventListener('submit', function(event) {
|
||||
if (!form.checkValidity()) {
|
||||
event.preventDefault();
|
||||
@@ -230,32 +231,71 @@
|
||||
|
||||
const typeDemandeRadios = document.querySelectorAll('input[name="typeDemande"]');
|
||||
const nouvelleLicenceBlock = document.getElementById('nouvelleLicenceBlock');
|
||||
|
||||
typeDemandeRadios.forEach(radio => {
|
||||
radio.addEventListener('change', function() {
|
||||
if (this.value === 'NOUVELLE') {
|
||||
nouvelleLicenceBlock.classList.remove('hidden');
|
||||
} else {
|
||||
nouvelleLicenceBlock.classList.add('hidden');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Trigger on load if already checked (e.g. going back or validation error)
|
||||
const checkedRadio = document.querySelector('input[name="typeDemande"]:checked');
|
||||
if (checkedRadio && checkedRadio.value === 'NOUVELLE') {
|
||||
nouvelleLicenceBlock.classList.remove('hidden');
|
||||
}
|
||||
|
||||
const ancienClubInput = document.getElementById('ancienClub');
|
||||
const ancienneCategorieBlock = document.getElementById('ancienneCategorieBlock');
|
||||
const ancienneCategorieSelect = document.getElementById('ancienneCategorie');
|
||||
const raisonChangementClubBlock = document.getElementById('raisonChangementClubBlock');
|
||||
const raisonChangementClubInput = document.getElementById('raisonChangementClub');
|
||||
const commentConnuInput = document.querySelector('input[name="commentConnuClub"]');
|
||||
|
||||
function updateTypeDemandeState(typeValue) {
|
||||
if (typeValue === 'NOUVELLE') {
|
||||
nouvelleLicenceBlock.classList.remove('hidden');
|
||||
// Synchroniser l'état de l'ancien club s'il contient du texte
|
||||
if (ancienClubInput && ancienClubInput.value.trim().length > 0) {
|
||||
ancienneCategorieBlock.classList.remove('hidden');
|
||||
ancienneCategorieSelect.required = true;
|
||||
raisonChangementClubBlock.classList.remove('hidden');
|
||||
raisonChangementClubInput.required = true;
|
||||
} else {
|
||||
ancienneCategorieBlock.classList.add('hidden');
|
||||
ancienneCategorieSelect.required = false;
|
||||
raisonChangementClubBlock.classList.add('hidden');
|
||||
raisonChangementClubInput.required = false;
|
||||
}
|
||||
} else {
|
||||
// RENOUVELLEMENT
|
||||
nouvelleLicenceBlock.classList.add('hidden');
|
||||
|
||||
// Réinitialisation complète des champs spécifiques aux nouvelles demandes
|
||||
if (ancienClubInput) ancienClubInput.value = '';
|
||||
if (raisonChangementClubInput) {
|
||||
raisonChangementClubInput.value = '';
|
||||
raisonChangementClubInput.required = false;
|
||||
}
|
||||
if (ancienneCategorieSelect) {
|
||||
ancienneCategorieSelect.value = 'NA';
|
||||
ancienneCategorieSelect.required = false;
|
||||
}
|
||||
if (commentConnuInput) commentConnuInput.value = '';
|
||||
|
||||
if (ancienneCategorieBlock) ancienneCategorieBlock.classList.add('hidden');
|
||||
if (raisonChangementClubBlock) raisonChangementClubBlock.classList.add('hidden');
|
||||
}
|
||||
|
||||
// Si le formulaire redevient valide, masquer le message d'erreur
|
||||
const errorMsg = document.getElementById('form-error-msg');
|
||||
if (errorMsg && form.checkValidity()) {
|
||||
errorMsg.remove();
|
||||
}
|
||||
}
|
||||
|
||||
typeDemandeRadios.forEach(radio => {
|
||||
radio.addEventListener('change', function() {
|
||||
updateTypeDemandeState(this.value);
|
||||
});
|
||||
});
|
||||
|
||||
// Exécution initiale au chargement si un type est déjà coché
|
||||
const checkedRadio = document.querySelector('input[name="typeDemande"]:checked');
|
||||
if (checkedRadio) {
|
||||
updateTypeDemandeState(checkedRadio.value);
|
||||
}
|
||||
|
||||
if (ancienClubInput) {
|
||||
ancienClubInput.addEventListener('input', function() {
|
||||
if (this.value.trim().length > 0) {
|
||||
const isNouvelle = document.querySelector('input[name="typeDemande"]:checked')?.value === 'NOUVELLE';
|
||||
if (isNouvelle && this.value.trim().length > 0) {
|
||||
ancienneCategorieBlock.classList.remove('hidden');
|
||||
ancienneCategorieSelect.required = true;
|
||||
raisonChangementClubBlock.classList.remove('hidden');
|
||||
@@ -267,35 +307,36 @@
|
||||
raisonChangementClubInput.required = false;
|
||||
}
|
||||
});
|
||||
// Initial check
|
||||
if (ancienClubInput.value.trim().length > 0) {
|
||||
ancienneCategorieBlock.classList.remove('hidden');
|
||||
ancienneCategorieSelect.required = true;
|
||||
raisonChangementClubBlock.classList.remove('hidden');
|
||||
raisonChangementClubInput.required = true;
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('dateNaissance').addEventListener('change', function() {
|
||||
const dateInput = this.value;
|
||||
const repBlock = document.getElementById('representantBlock');
|
||||
const repInput = document.getElementById('representantLegal');
|
||||
const dateNaissanceInput = document.getElementById('dateNaissance');
|
||||
if (dateNaissanceInput) {
|
||||
dateNaissanceInput.addEventListener('change', function() {
|
||||
const dateInput = this.value;
|
||||
const repBlock = document.getElementById('representantBlock');
|
||||
const repInput = document.getElementById('representantLegal');
|
||||
|
||||
if (dateInput) {
|
||||
const dob = new Date(dateInput);
|
||||
const ageDifMs = Date.now() - dob.getTime();
|
||||
const ageDate = new Date(ageDifMs);
|
||||
const age = Math.abs(ageDate.getUTCFullYear() - 1970);
|
||||
if (dateInput) {
|
||||
const dob = new Date(dateInput);
|
||||
const ageDifMs = Date.now() - dob.getTime();
|
||||
const ageDate = new Date(ageDifMs);
|
||||
const age = Math.abs(ageDate.getUTCFullYear() - 1970);
|
||||
|
||||
if (age < 18) {
|
||||
repBlock.classList.remove('hidden');
|
||||
repInput.required = true;
|
||||
} else {
|
||||
repBlock.classList.add('hidden');
|
||||
repInput.required = false;
|
||||
if (age < 18) {
|
||||
repBlock.classList.remove('hidden');
|
||||
repInput.required = true;
|
||||
} else {
|
||||
repBlock.classList.add('hidden');
|
||||
repInput.required = false;
|
||||
repInput.value = '';
|
||||
}
|
||||
}
|
||||
});
|
||||
// Vérification initiale si la date est pré-remplie
|
||||
if (dateNaissanceInput.value) {
|
||||
dateNaissanceInput.dispatchEvent(new Event('change'));
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user