Skip to content

Commit d698b6f

Browse files
committed
frontend+backend validation of organisation input consistent
handles case of white space at start/end of other organisation input and prevents form submission with helpful message
1 parent 2eb8f5a commit d698b6f

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

packages/divbase-api/src/divbase_api/static/js/organisation_dropdown.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ document.addEventListener("DOMContentLoaded", function () {
99
const otherOrganisationDiv = document.getElementById("other_organisation_div");
1010
const otherOrganisationInput = document.getElementById("organisation_other");
1111

12+
const form = document.getElementById("registerForm") || document.getElementById("editProfileForm");
13+
if (!form) {
14+
return;
15+
}
16+
17+
function validateOtherOrganisation() {
18+
const otherOrganisation = otherOrganisationInput.value.trim();
19+
if (
20+
organisationDropdown.value === "Other" &&
21+
(!otherOrganisation || otherOrganisation.length < 3)
22+
) {
23+
otherOrganisationInput.setCustomValidity("Please specify your organisation (at least 3 characters).");
24+
} else {
25+
otherOrganisationInput.setCustomValidity("");
26+
}
27+
}
1228

1329
organisationDropdown.addEventListener("change", function () {
1430
if (this.value === "Other") {
@@ -17,9 +33,23 @@ document.addEventListener("DOMContentLoaded", function () {
1733
} else {
1834
otherOrganisationDiv.style.display = "none";
1935
otherOrganisationInput.required = false;
20-
otherOrganisationInput.value = ""; // Clears the value if hidden
36+
otherOrganisationInput.value = "";
37+
otherOrganisationInput.setCustomValidity("");
2138
}
2239
});
40+
41+
otherOrganisationInput.addEventListener("input", validateOtherOrganisation);
42+
43+
// Validation for "Other" organisation input on form submission will prevent form submission
44+
form.addEventListener("submit", function (event) {
45+
validateOtherOrganisation();
46+
47+
if (!form.checkValidity()) {
48+
form.reportValidity();
49+
event.preventDefault();
50+
}
51+
});
52+
2353
// Run on page load to ensure form is in correct state if form submission fails
2454
// and page/form is re-rendered with previous values.
2555
// e.g. password don't match etc...

packages/divbase-api/src/divbase_api/templates/profile_pages/edit_profile.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ <h2><i class="bi bi-pencil me-2"></i>Edit Profile</h2>
1818
<h5 class="mb-0"><i class="bi bi-person-gear me-2"></i>Personal Information</h5>
1919
</div>
2020
<div class="card-body">
21-
<form method="post" action="/profile/edit">
21+
<form method="post" action="/profile/edit" id="editProfileForm">
2222
<div class="row mb-3">
2323
<div class="col-12">
2424
<label for="name" class="form-label">Full Name</label>

0 commit comments

Comments
 (0)