From 298fd50da5ee4a626283cf1ab6a7fb82c04e9def Mon Sep 17 00:00:00 2001 From: adrien guernier Date: Thu, 5 Oct 2023 17:23:41 +0200 Subject: [PATCH 1/2] return undefined if field value is empty in useUnique --- packages/ra-core/src/form/useUnique.spec.tsx | 16 ++++++++++++++++ packages/ra-core/src/form/useUnique.ts | 3 +++ 2 files changed, 19 insertions(+) diff --git a/packages/ra-core/src/form/useUnique.spec.tsx b/packages/ra-core/src/form/useUnique.spec.tsx index c30f4544aa2..82400fa887a 100644 --- a/packages/ra-core/src/form/useUnique.spec.tsx +++ b/packages/ra-core/src/form/useUnique.spec.tsx @@ -258,4 +258,20 @@ describe('useUnique', () => { fireEvent.click(screen.getByText('Submit')); expect(screen.queryByText('Must be unique')).toBeNull(); }); + + it('should not show an error when the field value is empty', async () => { + const dataProvider = baseDataProvider(); + render(); + + const input = await screen.findByDisplayValue('John Doe'); + fireEvent.change(input, { target: { value: '' } }); + fireEvent.click(screen.getByText('Submit')); + + await waitFor(() => { + expect(dataProvider.create).toHaveBeenCalled(); + }); + + expect(dataProvider.getList).not.toHaveBeenCalled(); + expect(screen.queryByText('Must be unique')).toBeNull(); + }); }); diff --git a/packages/ra-core/src/form/useUnique.ts b/packages/ra-core/src/form/useUnique.ts index c75861909a8..ca8650a7309 100644 --- a/packages/ra-core/src/form/useUnique.ts +++ b/packages/ra-core/src/form/useUnique.ts @@ -87,6 +87,9 @@ export const useUnique = (options?: UseUniqueOptions) => { ); return async (value: any, allValues: any, props: InputProps) => { + if (!value) { + return undefined; + } try { const finalFilter = set( merge({}, filter), From c45a8d14bc616b8774a4d617b65fd2dd2138b7f8 Mon Sep 17 00:00:00 2001 From: adrien guernier Date: Fri, 6 Oct 2023 09:55:38 +0200 Subject: [PATCH 2/2] check if value is empty with isEmpty function --- packages/ra-core/src/form/useUnique.ts | 3 ++- packages/ra-core/src/form/validate.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/ra-core/src/form/useUnique.ts b/packages/ra-core/src/form/useUnique.ts index ca8650a7309..221a41c3059 100644 --- a/packages/ra-core/src/form/useUnique.ts +++ b/packages/ra-core/src/form/useUnique.ts @@ -7,6 +7,7 @@ import { useCallback, useRef } from 'react'; import set from 'lodash/set'; import { asyncDebounce } from '../util'; import { useRecordContext } from '../controller'; +import { isEmpty } from './validate'; /** * A hook that returns a validation function checking for a record field uniqueness @@ -87,7 +88,7 @@ export const useUnique = (options?: UseUniqueOptions) => { ); return async (value: any, allValues: any, props: InputProps) => { - if (!value) { + if (isEmpty(value)) { return undefined; } try { diff --git a/packages/ra-core/src/form/validate.ts b/packages/ra-core/src/form/validate.ts index e865f69d546..33fa9ef332c 100644 --- a/packages/ra-core/src/form/validate.ts +++ b/packages/ra-core/src/form/validate.ts @@ -4,7 +4,7 @@ import lodashMemoize from 'lodash/memoize'; /* @link http://stackoverflow.com/questions/46155/validate-email-address-in-javascript */ const EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; // eslint-disable-line no-useless-escape -const isEmpty = (value: any) => +export const isEmpty = (value: any) => typeof value === 'undefined' || value === null || value === '' ||