Fix the Add functionality on the Other Contacts formset

This commit is contained in:
Rachid Mrad 2024-01-05 12:22:15 -05:00
parent 2a64230c91
commit 13151c9368
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
4 changed files with 17 additions and 7 deletions

View file

@ -331,10 +331,10 @@ function prepareDeleteButtons(formLabel) {
* it everywhere.
*/
(function prepareFormsetsForms() {
let formIdentifier = "form"
let repeatableForm = document.querySelectorAll(".repeatable-form");
let container = document.querySelector("#form-container");
let addButton = document.querySelector("#add-form");
let totalForms = document.querySelector("#id_form-TOTAL_FORMS");
let cloneIndex = 0;
let formLabel = '';
let isNameserversForm = document.title.includes("DNS name servers |");
@ -343,7 +343,12 @@ function prepareDeleteButtons(formLabel) {
formLabel = "Name server";
} else if ((document.title.includes("DS Data |")) || (document.title.includes("Key Data |"))) {
formLabel = "DS Data record";
} else if (document.title.includes("Other employees from your organization")) {
formLabel = "Organization contact";
container = document.querySelector("#other-employees");
formIdentifier = "other_contacts"
}
let totalForms = document.querySelector(`#id_${formIdentifier}-TOTAL_FORMS`);
// On load: Disable the add more button if we have 13 forms
if (isNameserversForm && document.querySelectorAll(".repeatable-form").length == 13) {
@ -360,7 +365,7 @@ function prepareDeleteButtons(formLabel) {
let forms = document.querySelectorAll(".repeatable-form");
let formNum = forms.length;
let newForm = repeatableForm[cloneIndex].cloneNode(true);
let formNumberRegex = RegExp(`form-(\\d){1}-`,'g');
let formNumberRegex = RegExp(`${formIdentifier}-(\\d){1}-`,'g');
let formLabelRegex = RegExp(`${formLabel} (\\d){1}`, 'g');
// For the eample on Nameservers
let formExampleRegex = RegExp(`ns(\\d){1}`, 'g');
@ -393,7 +398,8 @@ function prepareDeleteButtons(formLabel) {
}
formNum++;
newForm.innerHTML = newForm.innerHTML.replace(formNumberRegex, `form-${formNum-1}-`);
newForm.innerHTML = newForm.innerHTML.replace(formNumberRegex, `${formIdentifier}-${formNum-1}-`);
newForm.innerHTML = newForm.innerHTML.replace(formLabelRegex, `${formLabel} ${formNum}`);
newForm.innerHTML = newForm.innerHTML.replace(formExampleRegex, `ns${formNum}`);
container.insertBefore(newForm, addButton);
@ -402,7 +408,7 @@ function prepareDeleteButtons(formLabel) {
// Reset the values of each input to blank
inputs.forEach((input) => {
input.classList.remove("usa-input--error");
if (input.type === "text" || input.type === "number" || input.type === "password") {
if (input.type === "text" || input.type === "number" || input.type === "password" || input.type === "email" || input.type === "tel") {
input.value = ""; // Set the value to an empty string
} else if (input.type === "checkbox" || input.type === "radio") {

View file

@ -734,7 +734,7 @@ class BaseOtherContactsFormSet(RegistrarFormSet):
OtherContactsFormSet = forms.formset_factory(
OtherContactsForm,
extra=1,
extra=0,
absolute_max=1500, # django default; use `max_num` to limit entries
min_num=1,
validate_min=True,

View file

@ -34,7 +34,7 @@
{{ forms.1.management_form }}
{# forms.1 is a formset and this iterates over its forms #}
{% for form in forms.1.forms %}
<fieldset class="usa-fieldset">
<fieldset class="usa-fieldset repeatable-form">
<legend>
<h2>Organization contact {{ forloop.counter }} (optional)</h2>
</legend>
@ -62,7 +62,7 @@
</fieldset>
{% endfor %}
<button type="submit" name="submit_button" value="save" class="usa-button usa-button--unstyled">
<button type="button" class="usa-button usa-button--unstyled" id="add-form">
<svg class="usa-icon" aria-hidden="true" focusable="false" role="img" width="24" height="24">
<use xlink:href="{%static 'img/sprite.svg'%}#add_circle"></use>
</svg><span class="margin-left-05">Add another contact</span>

View file

@ -370,6 +370,10 @@ class ApplicationWizard(ApplicationWizardPermissionView, TemplateView):
def post(self, request, *args, **kwargs) -> HttpResponse:
"""This method handles POST requests."""
# Log the keys and values of request.POST
for key, value in request.POST.items():
logger.info("Key: %s, Value: %s", key, value)
# which button did the user press?
button: str = request.POST.get("submit_button", "")