16293 fix form data handler deleting checkbox data

In NetBox, formData may have multiple values associated with a given key
(see netbox/templates/django/forms/widgets/checkbox.html).

So instead of a for loop over all formData.entries(), retrieve all
values using formData.getAll(), delete all keys from formData, then
append all non-empty values back in.
This commit is contained in:
Iain Buclaw 2024-06-04 01:19:25 +02:00
parent 8e48e939aa
commit 3ec84e36d5

View File

@ -42,9 +42,15 @@ function initWindow(): void {
if (documentForm.method.toUpperCase() == 'GET') {
documentForm.addEventListener('formdata', function (event: FormDataEvent) {
const formData: FormData = event.formData;
for (const [name, value] of Array.from(formData.entries())) {
if (value === '') formData.delete(name);
}
// formData may have multiple values associated with a given key
// (see netbox/templates/django/forms/widgets/checkbox.html).
for (const name of Array.from(new Set(formData.keys()))) {
const values = formData.getAll(name);
formData.delete(name);
values.forEach(value => {
if (value !== '') formData.append(name, value)
});
}
});
}
}