Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useUnique send request even if the field value is empty #9334

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/ra-core/src/form/useUnique.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Create dataProvider={dataProvider} />);

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();
});
});
3 changes: 3 additions & 0 deletions packages/ra-core/src/form/useUnique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export const useUnique = (options?: UseUniqueOptions) => {
);

return async (value: any, allValues: any, props: InputProps) => {
if (!value) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract and use 'isEmptyfromvalidate.ts` instead?

return undefined;
}
try {
const finalFilter = set(
merge({}, filter),
Expand Down
Loading