commit 76701580ce8a44271bfe9d464bd7a1f668ea1b0d
parent d441802e074c7fe6e86dd0c019c7ef62babddf7e
Author: Florian Dold <florian@dold.me>
Date: Mon, 10 Mar 2025 15:29:37 +0100
web-util: Do not validate hidden form fields
Diffstat:
2 files changed, 18 insertions(+), 26 deletions(-)
diff --git a/packages/aml-backoffice-ui/src/pages/Search.tsx b/packages/aml-backoffice-ui/src/pages/Search.tsx
@@ -31,16 +31,12 @@ import {
Attention,
encodeCrockForURI,
FormDesign,
- FormErrors,
FormUI,
- FormValues,
InternationalizationAPI,
Loading,
- RecursivePartial,
Time,
UIFormElementConfig,
UIHandlerId,
- undefinedIfEmpty,
useExchangeApiContext,
useForm,
useTranslationContext,
diff --git a/packages/web-util/src/hooks/useForm.ts b/packages/web-util/src/hooks/useForm.ts
@@ -135,37 +135,28 @@ export function useForm<T>(
interface Tree<T> extends Record<string, Tree<T> | T> {}
/**
- * Use $path to get the value of $object
- * return $noFoundValue if the target property is undefined
- *
- * @param object
- * @param path
- * @param notFoundValue
- * @returns
+ * Use {@link path} to get the value of {@link object}.
+ * Return {@link fallbackValue} if the target property is undefined
*/
export function getValueFromPath<T>(
object: Tree<T> | undefined,
path: string[],
- notFoundValue?: T,
+ fallbackValue?: T,
): T | undefined {
if (path.length === 0) return object as T;
const [head, ...rest] = path;
if (!head) {
- return getValueFromPath(object, rest, notFoundValue);
+ return getValueFromPath(object, rest, fallbackValue);
}
if (object === undefined) {
- return notFoundValue;
+ return fallbackValue;
}
- return getValueFromPath(object[head] as Tree<T>, rest, notFoundValue);
+ return getValueFromPath(object[head] as Tree<T>, rest, fallbackValue);
}
/**
* Use $path to set the value $value into $object
* Don't modify $object, returns a new value
- * @param object
- * @param path
- * @param value
- * @returns
*/
function setValueIntoPath(object: any, path: string[], value: any): any {
if (path.length === 0) return value;
@@ -194,8 +185,8 @@ export function undefinedIfEmpty<T extends object | undefined>(
}
function validateRequiredFields<FormType>(
- form: object,
- config: FormDesign,
+ formData: object,
+ formDesign: FormDesign,
i18n: InternationalizationAPI,
): FormErrors<FormType> | undefined {
let result: FormErrors<FormType> | undefined = undefined;
@@ -205,7 +196,7 @@ function validateRequiredFields<FormType>(
return;
}
const path = formElement.id.split(".");
- const v = getValueFromPath(form as any, path);
+ const v = getValueFromPath(formData as any, path);
if (formElement.required && v === undefined) {
const e: ErrorAndLabel = {
label: formElement.label as TranslatedString,
@@ -237,19 +228,24 @@ function validateRequiredFields<FormType>(
}
}
- switch (config.type) {
+ switch (formDesign.type) {
case "double-column": {
- config.sections.forEach((sec) => {
+ formDesign.sections.forEach((sec) => {
+ const hidden = sec.hide ? sec.hide(formData) : undefined;
+ if (hidden) {
+ // Do not validate hidden fields
+ return;
+ }
sec.fields.forEach(checkIfRequiredFieldHasValue);
});
break;
}
case "single-column": {
- config.fields.forEach(checkIfRequiredFieldHasValue);
+ formDesign.fields.forEach(checkIfRequiredFieldHasValue);
break;
}
default: {
- assertUnreachable(config);
+ assertUnreachable(formDesign);
}
}