commit 40bb9776c791ead02d626e07f68ebb67a7dc7015
parent c10a1701d0845316aca13192d9f83cadce934ec4
Author: Sebastian <sebasjm@taler-systems.com>
Date: Thu, 26 Nov 2026 12:06:43 -0300
fix #9869 web-util: exclude invalid fields from form results
Diffstat:
3 files changed, 70 insertions(+), 4 deletions(-)
diff --git a/packages/web-util/src/forms/fields/InputArray.test.tsx b/packages/web-util/src/forms/fields/InputArray.test.tsx
@@ -199,7 +199,7 @@ test("array items can be added, edited, cancelled, and deleted", async () => {
view.getByRole("button", { name: "Delete" }).click();
(await view.findByRole("button", { name: "Delete item" })).click();
- await eventually(() => assert.deepEqual(result.people, []));
+ await eventually(() => assert.equal(result.people, undefined));
cleanup();
await window.happyDOM.abort();
diff --git a/packages/web-util/src/hooks/useForm.test.tsx b/packages/web-util/src/hooks/useForm.test.tsx
@@ -7,11 +7,16 @@
Foundation; either version 3, or (at your option) any later version.
*/
-import { setupI18n, TranslatedString } from "@gnu-taler/taler-util";
+import {
+ setupI18n,
+ TalerFormAttributes,
+ TranslatedString,
+} from "@gnu-taler/taler-util";
import assert from "node:assert/strict";
import test from "node:test";
import { Window } from "happy-dom";
import { h } from "preact";
+import { act } from "preact/test-utils";
import { FormState, useForm } from "./useForm.js";
import { FormDesign } from "../forms/forms-types.js";
@@ -175,3 +180,62 @@ test("missing fields and conflicting attribute paths fail explicitly", async ()
cleanup();
await window.happyDOM.abort();
});
+
+test("the result excludes invalid fields until they become valid", async () => {
+ const window = installDom();
+ const { cleanup, render } = await import("@testing-library/preact");
+ const futureDate = "2999-01-01";
+ const design: FormDesign = {
+ type: "single-column",
+ fields: [
+ {
+ id: TalerFormAttributes.FULL_NAME,
+ type: "text",
+ label: "Full name" as TranslatedString,
+ },
+ {
+ id: TalerFormAttributes.DATE_OF_BIRTH,
+ type: "isoDateText",
+ label: "Date of birth" as TranslatedString,
+ pattern: "dd/MM/yyyy",
+ validator(value) {
+ return new Date(value) > new Date()
+ ? ("it can't be in the future" as TranslatedString)
+ : undefined;
+ },
+ },
+ ],
+ };
+ let form: FormState<Record<string, string>> | undefined;
+
+ function Harness() {
+ form = useForm<Record<string, string>>(design, {
+ [TalerFormAttributes.FULL_NAME]: "Alice Example",
+ [TalerFormAttributes.DATE_OF_BIRTH]: futureDate,
+ });
+ return null;
+ }
+
+ render(<Harness />);
+ assert.ok(form);
+ assert.equal(form.status.status === "fail", true);
+ assert.equal(
+ form.fields[TalerFormAttributes.DATE_OF_BIRTH].value,
+ futureDate,
+ );
+ assert.deepEqual(form.status.result, {
+ [TalerFormAttributes.FULL_NAME]: "Alice Example",
+ });
+
+ await act(() => {
+ form!.fields[TalerFormAttributes.DATE_OF_BIRTH].onChange("2000-01-02");
+ });
+ assert.equal(form.status.status === "ok", true);
+ assert.deepEqual(form.status.result, {
+ [TalerFormAttributes.FULL_NAME]: "Alice Example",
+ [TalerFormAttributes.DATE_OF_BIRTH]: "2000-01-02",
+ });
+
+ cleanup();
+ await window.happyDOM.abort();
+});
diff --git a/packages/web-util/src/hooks/useForm.ts b/packages/web-util/src/hooks/useForm.ts
@@ -511,7 +511,7 @@ function constructFormHandler<T>(
error: currentError?.message,
hidden,
};
- if (!hidden) {
+ if (!hidden && currentError === undefined) {
result = setValueIntoPath(result, path, controller.value) ?? {};
}
} else {
@@ -601,7 +601,9 @@ function constructFormHandler<T>(
};
controllers.set(field.name, controller);
model.registerUiField(`accept-tos.${field.name}`, handler);
- result = setValueIntoPath(result, path, currentValue) ?? {};
+ if (currentError === undefined) {
+ result = setValueIntoPath(result, path, currentValue) ?? {};
+ }
}
break;
}