fix(web): skip fields of disabled fieldsets when submitting forms
The serializer checked the disabled property, which only reflects a field's own attribute, so settings of other monitor types were sent too and fields with the same name overwrote each other. Forms also gain arrays from numbered names and header lines. Browser tests now run every page in testdata, with a time limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
5a76702dba
commit
8fe56546a0
3 files changed
+101
-22
No files matched your search
@@ -20,24 +20,31 @@
|
||||
// Inputs inside a hidden [data-step] are skipped. Checkboxes become
|
||||
// booleans, or arrays of values with [data-multi]; [data-list] inputs are
|
||||
// split on commas, [data-number] parsed and empty [data-nullable] sent as null.
|
||||
// [data-headers] textareas become objects from "Name: value" lines.
|
||||
// Dotted names such as "config.keyword" build nested objects.
|
||||
const serialize = (form) => {
|
||||
const out = {};
|
||||
// Numeric segments build arrays: "config.assertions.0.path".
|
||||
const set = (name, value) => {
|
||||
const path = name.split(".");
|
||||
let obj = out;
|
||||
for (const key of path.slice(0, -1)) obj = obj[key] ??= {};
|
||||
path.slice(0, -1).forEach((key, i) => {
|
||||
obj = obj[key] ??= /^\d+$/.test(path[i + 1]) ? [] : {};
|
||||
});
|
||||
obj[path.at(-1)] = value;
|
||||
};
|
||||
const get = (name) => name.split(".").reduce((o, k) => o?.[k], out);
|
||||
for (const el of form.elements) {
|
||||
if (!el.name || el.type === "fieldset" || el.closest("[data-step]")?.hidden) continue;
|
||||
// :disabled also covers fields inside a disabled fieldset, such as
|
||||
// the settings of other monitor types.
|
||||
const disabled = el.matches(":disabled");
|
||||
if (el.dataset.multi !== undefined) {
|
||||
if (get(el.name) === undefined) set(el.name, []);
|
||||
if (el.checked && !el.disabled) get(el.name).push(el.value);
|
||||
if (el.checked && !disabled) get(el.name).push(el.value);
|
||||
continue;
|
||||
}
|
||||
if (el.disabled) continue;
|
||||
if (disabled) continue;
|
||||
// "<resource_type>:<id>" limits an assignment to one resource.
|
||||
if (el.dataset.scope !== undefined) {
|
||||
const [type, id] = el.value.split(":");
|
||||
@@ -50,6 +57,10 @@
|
||||
if (el.type === "checkbox") set(el.name, el.checked);
|
||||
else if (el.dataset.number !== undefined) set(el.name, Number(el.value));
|
||||
else if (el.dataset.nullable !== undefined) set(el.name, el.value || null);
|
||||
else if (el.dataset.headers !== undefined)
|
||||
set(el.name, Object.fromEntries(
|
||||
el.value.split("\n").map((l) => l.split(":")).filter((p) => p.length > 1 && p[0].trim())
|
||||
.map(([k, ...v]) => [k.trim(), v.join(":").trim()])));
|
||||
else if (el.dataset.list !== undefined)
|
||||
set(el.name, el.value.split(",").map((v) => v.trim()).filter(Boolean));
|
||||
else set(el.name, el.value);
|
||||
|
||||
Reference in new issue
Block a user