From 9f649e0708c395b6a6ffbaff7fa46c4d296b578c Mon Sep 17 00:00:00 2001 From: Thomas Dax Date: Fri, 6 Sep 2024 10:00:52 +0200 Subject: [PATCH] Update required validator in Field to correctly handle falsey values (v6) (#2500) Backport of https://github.com/vivid-planet/comet/pull/2499 --- .changeset/quiet-bats-mate.md | 8 ++++++++ packages/admin/admin/src/form/Field.tsx | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changeset/quiet-bats-mate.md diff --git a/.changeset/quiet-bats-mate.md b/.changeset/quiet-bats-mate.md new file mode 100644 index 0000000000..674f7f29ee --- /dev/null +++ b/.changeset/quiet-bats-mate.md @@ -0,0 +1,8 @@ +--- +"@comet/admin": patch +--- + +Update required validator in `Field` to correctly handle falsey values + +Previously, the validator incorrectly returned errors for all falsey values, e.g. the number `0`. +Now, it only returns an error for `undefined`, `null`, `false` and empty strings. diff --git a/packages/admin/admin/src/form/Field.tsx b/packages/admin/admin/src/form/Field.tsx index b3cbf3678b..ef48daaa13 100644 --- a/packages/admin/admin/src/form/Field.tsx +++ b/packages/admin/admin/src/form/Field.tsx @@ -6,7 +6,12 @@ import { FormattedMessage } from "react-intl"; import { FieldContainer, FieldContainerProps } from "./FieldContainer"; import { useFinalFormContext } from "./FinalFormContextProvider"; -const requiredValidator = (value: any) => (value ? undefined : ); +const requiredValidator = (value: any) => { + if (value === undefined || value === null || value === false || value === "") { + return ; + } + return undefined; +}; const composeValidators = (...validators: Array<(value: any, allValues: object) => any>) =>