remove errors from being returned if forms are deleted, js to hide deleted forms on page load

This commit is contained in:
David Kennedy 2024-01-08 06:33:26 -05:00
parent dcce354224
commit e90474cab1
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
2 changed files with 38 additions and 5 deletions

View file

@ -463,9 +463,26 @@ function prepareDeleteButtons(formLabel) {
}
});
}
/**
* On form load, hide deleted forms, ie. those forms with hidden input of class 'deletion'
* with value='on'
*/
function hideDeletedForms() {
let hiddenDeleteButtonsWithValueOn = document.querySelectorAll('input[type="hidden"].deletion[value="on"]');
// Iterating over the NodeList of hidden inputs
hiddenDeleteButtonsWithValueOn.forEach(function(hiddenInput) {
// Finding the closest parent element with class "repeatable-form" for each hidden input
var repeatableFormToHide = hiddenInput.closest('.repeatable-form');
// Checking if a matching parent element is found for each hidden input
if (repeatableFormToHide) {
// Setting the display property to "none" for each matching parent element
repeatableFormToHide.style.display = 'none';
}
});
}
/**
@ -500,6 +517,9 @@ function prepareDeleteButtons(formLabel) {
addButton.setAttribute("disabled", "true");
}
// Hide forms which have previously been deleted
hideDeletedForms()
// Attach click event listener on the delete buttons of the existing forms
prepareDeleteButtons(formLabel);

View file

@ -780,12 +780,12 @@ class OtherContactsForm(RegistrarForm):
"""
This method overrides the default behavior for forms.
This cleans the form after field validation has already taken place.
In this override, allow for a form which is empty to be considered
valid even though certain required fields have not passed field
validation
In this override, allow for a form which is empty, or one marked for
deletion to be considered valid even though certain required fields have
not passed field validation
"""
if self.form_data_marked_for_deletion:
if self.form_data_marked_for_deletion or self.cleaned_data["DELETE"]:
# clear any errors raised by the form fields
# (before this clean() method is run, each field
# performs its own clean, which could result in
@ -810,6 +810,19 @@ class OtherContactsForm(RegistrarForm):
class BaseOtherContactsFormSet(RegistrarFormSet):
"""
FormSet for Other Contacts
There are two conditions by which a form in the formset can be marked for deletion.
One is if the user clicks 'DELETE' button, and this is submitted in the form. The
other is if the YesNo form, which is submitted with this formset, is set to No; in
this case, all forms in formset are marked for deletion. Both of these conditions
must co-exist.
Also, other_contacts have db relationships to multiple db objects. When attempting
to delete an other_contact from an application, those db relationships must be
tested and handled; this is configured with REVERSE_JOINS, which is an array of
strings representing the relationships between contact model and other models.
"""
JOIN = "other_contacts"
REVERSE_JOINS = [
"user",