|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "useUnique" |
| 4 | +--- |
| 5 | + |
| 6 | +# `useUnique` |
| 7 | + |
| 8 | +Validating the uniqueness of a field is a common requirement so React-admin provides the `useUnique` hook that returns a validator for this use case. |
| 9 | + |
| 10 | +It will call the [`dataProvider.getList`](./DataProviderWriting.md#request-format) method with a filter to check whether a record exists with the current value of the input for the field matching the input source. |
| 11 | + |
| 12 | +<video controls autoplay playsinline muted loop> |
| 13 | + <source src="./img/useUnique.webm" type="video/webm"/> |
| 14 | + <source src="./img/useUnique.mp4" type="video/mp4"/> |
| 15 | + Your browser does not support the video tag. |
| 16 | +</video> |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +```js |
| 21 | +import { SimpleForm, TextInput, useUnique } from 'react-admin'; |
| 22 | + |
| 23 | +const UserCreateForm = () => { |
| 24 | + const unique = useUnique(); |
| 25 | + return ( |
| 26 | + <SimpleForm> |
| 27 | + <TextInput source="username" validate={unique()} /> |
| 28 | + </SimpleForm> |
| 29 | + ); |
| 30 | +}; |
| 31 | +``` |
| 32 | + |
| 33 | +## Options |
| 34 | + |
| 35 | +| Option | Required | Type | Default | Description | |
| 36 | +| ------------------- | -------- | -------------- | -------- | ---------------------------------------------------------------------------------- | |
| 37 | +| `message` | Optional | `string` | `ra.validation.unique` | A custom message to display when the validation fails | |
| 38 | +| `debounce` | Optional | `number` | 1000 | The number of milliseconds to wait for new changes before validating | |
| 39 | +| `filter` | Optional | `object` | - | Additional filters to pass to the `dataProvider.getList` call | |
| 40 | +| `resource` | Optional | `string` | current from Context | The resource targeted by the `dataProvider.getList` call | |
| 41 | + |
| 42 | +## `message` |
| 43 | + |
| 44 | +A custom message to display when the validation fails. Defaults to `Must be unique` (translation key: `ra.validation.unique`). |
| 45 | +It accepts a translation key. The [`translate` function](./useTranslate.md) will be called with the following parameters: |
| 46 | +- `source`: the input name |
| 47 | +- `label`: the translated input label |
| 48 | +- `value`: the current input value |
| 49 | + |
| 50 | +```jsx |
| 51 | +import { SimpleForm, TextInput, useUnique } from 'react-admin'; |
| 52 | +import polyglotI18nProvider from 'ra-i18n-polyglot'; |
| 53 | + |
| 54 | +const i18nProvider = polyglotI18nProvider(() => |
| 55 | + mergeTranslations(englishMessages, { |
| 56 | + myapp: { |
| 57 | + validation: { |
| 58 | + unique: 'Value %{value} is already used for %{field}', |
| 59 | + }, |
| 60 | + }, |
| 61 | + }) |
| 62 | +); |
| 63 | + |
| 64 | +const UserCreateForm = () => { |
| 65 | + const unique = useUnique(); |
| 66 | + return ( |
| 67 | + <SimpleForm> |
| 68 | + <TextInput source="username" validate={unique({ message: 'myapp.validation.unique' })} /> |
| 69 | + </SimpleForm> |
| 70 | + ); |
| 71 | +}; |
| 72 | +``` |
| 73 | + |
| 74 | +## `debounce` |
| 75 | + |
| 76 | +The number of milliseconds to wait for new changes before actually calling the [`dataProvider.getList`](./DataProviderWriting.md#request-format) method. |
| 77 | + |
| 78 | + |
| 79 | +```jsx |
| 80 | +import { SimpleForm, TextInput, useUnique } from 'react-admin'; |
| 81 | + |
| 82 | +const UserCreateForm = () => { |
| 83 | + const unique = useUnique(); |
| 84 | + return ( |
| 85 | + <SimpleForm> |
| 86 | + <TextInput source="username" validate={unique({ debounce: 2000 })} /> |
| 87 | + </SimpleForm> |
| 88 | + ); |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | +## `resource` |
| 93 | + |
| 94 | +The resource targeted by the [`dataProvider.getList`](./DataProviderWriting.md#request-format) call. Defaults to the resource from the nearest [`ResourceContext`](./Resource.md#resource-context). |
| 95 | + |
| 96 | +This can be useful for custom pages instead of setting up a [`ResourceContext`](./Resource.md#resource-context). |
| 97 | + |
| 98 | +```jsx |
| 99 | +import { PasswordInput, SimpleForm, TextInput, useUnique } from 'react-admin'; |
| 100 | + |
| 101 | +const UserCreateForm = () => { |
| 102 | + const unique = useUnique(); |
| 103 | + return ( |
| 104 | + <SimpleForm> |
| 105 | + <TextInput source="username" validate={unique({ resource: 'users' })} /> |
| 106 | + <PasswordInput source="password" /> |
| 107 | + </SimpleForm> |
| 108 | + ); |
| 109 | +}; |
| 110 | +``` |
| 111 | + |
| 112 | +## `filter` |
| 113 | + |
| 114 | +Additional filters to pass to the [`dataProvider.getList`](./DataProviderWriting.md#request-format) method. This is useful when the value should be unique across a subset of the resource records, for instance, usernames in an organization: |
| 115 | + |
| 116 | +```jsx |
| 117 | +import { FormDataConsumer, ReferenceInput, SimpleForm, TextInput, useUnique } from 'react-admin'; |
| 118 | + |
| 119 | +const UserCreateForm = () => { |
| 120 | + const unique = useUnique(); |
| 121 | + return ( |
| 122 | + <SimpleForm> |
| 123 | + <ReferenceInput source="organization_id" reference="organizations"> |
| 124 | + <FormDataConsumer> |
| 125 | + {({ formData }) => ( |
| 126 | + <TextInput |
| 127 | + source="username" |
| 128 | + validate={unique({ |
| 129 | + filter: { |
| 130 | + organization_id: formData.organization_id, |
| 131 | + }, |
| 132 | + })} |
| 133 | + /> |
| 134 | + )} |
| 135 | + </FormDataConsumer> |
| 136 | + </SimpleForm> |
| 137 | + ); |
| 138 | +}; |
| 139 | +``` |
0 commit comments