Skip to content

Commit

Permalink
Merge pull request #9637 from marmelab/referenceinput-validate
Browse files Browse the repository at this point in the history
Fix `<ReferenceInput>` accepts a validate prop while it should not
  • Loading branch information
slax57 committed Feb 9, 2024
2 parents 062b143 + 1723375 commit 19f982e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/ra-ui-materialui/src/input/ReferenceInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,23 @@ describe('<ReferenceInput />', () => {
screen.getByLabelText('Save').click();
await screen.findByText('Proust', undefined, { timeout: 5000 });
});

it('should throw an error when using the validate prop', () => {
jest.spyOn(console, 'error').mockImplementation(jest.fn());
const dataProvider = testDataProvider({
getList: async () => ({ data: [], total: 25 }),
});
expect(() =>
render(
<CoreAdminContext dataProvider={dataProvider}>
<Form>
<ReferenceInput
{...defaultProps}
validate={() => undefined}
/>
</Form>
</CoreAdminContext>
)
).toThrowError();
});
});
5 changes: 5 additions & 0 deletions packages/ra-ui-materialui/src/input/ReferenceInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export const ReferenceInput = (props: ReferenceInputProps) => {
filter,
});

if (props.validate) {
throw new Error(
'<ReferenceInput> does not accept a validate prop. Set the validate prop on the child instead.'
);
}
if (Children.count(children) !== 1) {
throw new Error('<ReferenceInput> only accepts a single child');
}
Expand Down

0 comments on commit 19f982e

Please sign in to comment.