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

Add unique validator #8999

Merged
merged 16 commits into from
Jun 16, 2023
Merged

Add unique validator #8999

merged 16 commits into from
Jun 16, 2023

Conversation

djhi
Copy link
Collaborator

@djhi djhi commented Jun 12, 2023

Validating the uniqueness of a field is a common requirement so we'd like to provide the useUnique hook that returns a validator for this use case.

It will call the dataProvider.getList method with a filter to check whether a record exists with the current value of the input for the field matching the input source.

Basic usage (more options in the documentation):

const UserCreateForm = () => {
    const unique = useUnique();
    return (
        <SimpleForm>
            <TextInput source="username" validate={unique()} />
        </SimpleForm>
    );
};

docs/Validation.md Outdated Show resolved Hide resolved
docs/Validation.md Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.ts Outdated Show resolved Hide resolved
docs/Validation.md Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.ts Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.spec.tsx Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.spec.tsx Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.spec.tsx Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.spec.tsx Show resolved Hide resolved
packages/ra-core/src/form/useUnique.stories.tsx Outdated Show resolved Hide resolved
@fzaninotto
Copy link
Member

You should update the doc sidebar to add an entry for the new useUnique hook.

docs/useUnique.md Outdated Show resolved Hide resolved
docs/useUnique.md Show resolved Hide resolved
docs/useUnique.md Outdated Show resolved Hide resolved
docs/useUnique.md Show resolved Hide resolved
docs/useUnique.md Show resolved Hide resolved
packages/ra-core/src/form/useUnique.spec.tsx Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.spec.tsx Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.ts Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.spec.tsx Show resolved Hide resolved
packages/ra-core/src/form/useUnique.ts Show resolved Hide resolved
Co-authored-by: Francois Zaninotto <francois@marmelab.com>
docs/useUnique.md Outdated Show resolved Hide resolved
docs/useUnique.md Outdated Show resolved Hide resolved
docs/useUnique.md Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.stories.tsx Outdated Show resolved Hide resolved
packages/ra-core/src/form/useUnique.ts Outdated Show resolved Hide resolved
packages/ra-core/src/util/asyncDebounce.ts Outdated Show resolved Hide resolved
Co-authored-by: Jean-Baptiste Kaiser <jb@marmelab.com>
@fzaninotto fzaninotto merged commit 5dedc3a into next Jun 16, 2023
@fzaninotto fzaninotto deleted the unique-validator branch June 16, 2023 13:43
@fzaninotto fzaninotto added this to the 4.12.0 milestone Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants