actually use assignValue or rather serializeFieldRecursive

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Jens Langhammer 2023-10-02 21:44:37 +02:00
parent cdf5aca378
commit de2db0704c
No known key found for this signature in database
1 changed files with 14 additions and 10 deletions

View File

@ -197,17 +197,21 @@ export abstract class Form<T> extends AKElement {
"multiple" in inputElement.attributes "multiple" in inputElement.attributes
) { ) {
const selectElement = inputElement as unknown as HTMLSelectElement; const selectElement = inputElement as unknown as HTMLSelectElement;
json[element.name] = Array.from(selectElement.selectedOptions).map((v) => v.value); this.assignValue(
inputElement,
Array.from(selectElement.selectedOptions).map((v) => v.value),
json,
);
} else if ( } else if (
inputElement.tagName.toLowerCase() === "input" && inputElement.tagName.toLowerCase() === "input" &&
inputElement.type === "date" inputElement.type === "date"
) { ) {
json[element.name] = inputElement.valueAsDate; this.assignValue(inputElement, inputElement.valueAsDate, json);
} else if ( } else if (
inputElement.tagName.toLowerCase() === "input" && inputElement.tagName.toLowerCase() === "input" &&
inputElement.type === "datetime-local" inputElement.type === "datetime-local"
) { ) {
json[element.name] = new Date(inputElement.valueAsNumber); this.assignValue(inputElement, new Date(inputElement.valueAsNumber), json);
} else if ( } else if (
inputElement.tagName.toLowerCase() === "input" && inputElement.tagName.toLowerCase() === "input" &&
"type" in inputElement.dataset && "type" in inputElement.dataset &&
@ -215,19 +219,19 @@ export abstract class Form<T> extends AKElement {
) { ) {
// Workaround for Firefox <93, since 92 and older don't support // Workaround for Firefox <93, since 92 and older don't support
// datetime-local fields // datetime-local fields
json[element.name] = new Date(inputElement.value); this.assignValue(inputElement, new Date(inputElement.value), json);
} else if ( } else if (
inputElement.tagName.toLowerCase() === "input" && inputElement.tagName.toLowerCase() === "input" &&
inputElement.type === "checkbox" inputElement.type === "checkbox"
) { ) {
json[element.name] = inputElement.checked; this.assignValue(inputElement, inputElement.checked, json);
} else if ("selectedFlow" in inputElement) { } else if ("selectedFlow" in inputElement) {
json[element.name] = inputElement.value; this.assignValue(inputElement, inputElement.value, json);
} else if (inputElement.tagName.toLowerCase() === "ak-search-select") { } else if (inputElement.tagName.toLowerCase() === "ak-search-select") {
const select = inputElement as unknown as SearchSelect<unknown>; const select = inputElement as unknown as SearchSelect<unknown>;
try { try {
const value = select.toForm(); const value = select.toForm();
json[element.name] = value; this.assignValue(inputElement, value, json);
} catch (exc) { } catch (exc) {
if (exc instanceof PreventFormSubmit) { if (exc instanceof PreventFormSubmit) {
throw new PreventFormSubmit(exc.message, element); throw new PreventFormSubmit(exc.message, element);
@ -235,16 +239,16 @@ export abstract class Form<T> extends AKElement {
throw exc; throw exc;
} }
} else { } else {
this.serializeFieldRecursive(inputElement, inputElement.value, json); this.assignValue(inputElement, inputElement.value, json);
} }
}); });
return json as unknown as T; return json as unknown as T;
} }
/** /**
* As far as anyone can remember, this isn't being used. * Recursively assign `value` into `json` while interpreting the dot-path of `element.name`
*/ */
private serializeFieldRecursive( private assignValue(
element: HTMLInputElement, element: HTMLInputElement,
value: unknown, value: unknown,
json: { [key: string]: unknown }, json: { [key: string]: unknown },