Skip to content

Commit 7a24921

Browse files
authored
Merge pull request #11104 from marmelab/fix-docs-validate-prop
[Doc] Clarify validation prop usage on ReferenceInput/ReferenceArrayInput
2 parents 2fceff3 + 4e3d276 commit 7a24921

8 files changed

Lines changed: 44 additions & 6 deletions

docs/AutocompleteArrayInput.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ If you need to *fetch* the options from another resource, you're actually editin
126126
</ReferenceArrayInput>
127127
```
128128

129+
**Tip**: If you need validation (e.g. `required()`), put the `validate` prop on the child `<AutocompleteArrayInput>`. `<ReferenceArrayInput>` doesn't accept validation props.
130+
129131
You can also pass an *array of strings* for the choices:
130132

131133
```jsx
@@ -839,4 +841,3 @@ const CreateTag = () => {
839841
};
840842
```
841843
{% endraw %}
842-

docs/AutocompleteInput.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,19 +764,22 @@ const CompanyInput = () => {
764764
As this is a common task, react-admin provides a shortcut to do the same in a declarative way: [`<ReferenceInput>`](./ReferenceInput.md):
765765

766766
```jsx
767-
import { ReferenceInput, AutocompleteInput } from 'react-admin';
767+
import { ReferenceInput, AutocompleteInput, required } from 'react-admin';
768768

769769
const CompanyInput = () => (
770770
<ReferenceInput reference="companies" source="company_id">
771771
<AutocompleteInput
772772
label="Company"
773773
source="company_id"
774774
optionText="name"
775+
validate={required()}
775776
/>
776777
</ReferenceInput>
777778
);
778779
```
779780

781+
**Tip**: If you need validation (e.g. `required()`), put the `validate` prop on the child input (`<AutocompleteInput>`). `<ReferenceInput>` doesn't accept validation props.
782+
780783
`<ReferenceInput>` is a headless component that:
781784

782785
- fetches a list of records with `dataProvider.getList()` and `dataProvider.getOne()`, using the `reference` prop for the resource,

docs/DualListInput.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ If you need to *fetch* the options from another resource, you're actually editin
134134
</ReferenceArrayInput>
135135
```
136136

137+
**Tip**: If you need validation (e.g. `required()`), put the `validate` prop on the child `<DualListInput>`. `<ReferenceArrayInput>` doesn't accept validation props.
138+
137139
If you have an *array of values* for the options, turn it into an array of objects with the `id` and `name` properties:
138140

139141
```jsx

docs/RadioButtonGroupInput.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,19 +386,22 @@ const CompanyInput = () => {
386386
As this is a common task, react-admin provides a shortcut to do the same in a declarative way: [`<ReferenceInput>`](./ReferenceInput.md):
387387

388388
```jsx
389-
import { ReferenceInput, RadioButtonGroupInput } from 'react-admin';
389+
import { ReferenceInput, RadioButtonGroupInput, required } from 'react-admin';
390390

391391
const CompanyInput = () => (
392392
<ReferenceInput reference="companies" source="company_id">
393393
<RadioButtonGroupInput
394394
label="Company"
395395
source="company_id"
396396
optionText="name"
397+
validate={required()}
397398
/>
398399
</ReferenceInput>
399400
);
400401
```
401402

403+
**Tip**: If you need validation (e.g. `required()`), put the `validate` prop on the child `<RadioButtonGroupInput>`. `<ReferenceInput>` doesn't accept validation props.
404+
402405
`<ReferenceInput>` is a headless component that:
403406

404407
- fetches a list of records with `dataProvider.getList()` and `dataProvider.getOne()`, using the `reference` prop for the resource,

docs/ReferenceArrayInput.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,19 @@ See the [`children`](#children) section for more details.
111111
| `queryOptions` | Optional | [`UseQueryOptions`](https://tanstack.com/query/v5/docs/react/reference/useQuery) | `{}` | `react-query` client options |
112112
| `sort` | Optional | `{ field: String, order: 'ASC' or 'DESC' }` | `{ field: 'id', order: 'DESC' }` | How to order the list of suggestions |
113113

114-
**Note**: `<ReferenceArrayInput>` doesn't accept the [common input props](./Inputs.md#common-input-props) ; it is the responsability of children to apply them.
114+
**Note**: `<ReferenceArrayInput>` doesn't accept the [common input props](./Inputs.md#common-input-props) ; it is the responsability of children to apply them. The same is true for validation: put the `validate` prop on the child input (`<AutocompleteArrayInput>`, `<SelectArrayInput>`, `<DualListInput>`, etc.), not on `<ReferenceArrayInput>`.
115+
116+
## Validation
117+
118+
`<ReferenceArrayInput>` doesn't accept a `validate` prop. Put validation on the child input instead (`<AutocompleteArrayInput>`, `<SelectArrayInput>`, `<DualListInput>`, etc.).
119+
120+
```jsx
121+
import { ReferenceArrayInput, SelectArrayInput, required } from 'react-admin';
122+
123+
<ReferenceArrayInput source="tag_ids" reference="tags">
124+
<SelectArrayInput validate={required()} />
125+
</ReferenceArrayInput>
126+
```
115127

116128
## `children`
117129

docs/ReferenceInput.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,19 @@ See the [`children`](#children) section for more details.
114114
| `queryOptions` | Optional | [`UseQueryOptions`](https://tanstack.com/query/v5/docs/react/reference/useQuery) | `{}` | `react-query` client options |
115115
| `sort` | Optional | `{ field: String, order: 'ASC' or 'DESC' }` | `{ field:'id', order:'DESC' }` | How to order the list of suggestions |
116116

117-
**Note**: `<ReferenceInput>` doesn't accept the [common input props](./Inputs.md#common-input-props) (like `label`) ; it is the responsibility of the child component to apply them.
117+
**Note**: `<ReferenceInput>` doesn't accept the [common input props](./Inputs.md#common-input-props) (like `label`) ; it is the responsibility of the child component to apply them. The same goes for validation: pass `validate` to the child input (`<AutocompleteInput>`, `<SelectInput>`, `<RadioButtonGroupInput>`, etc.), not to `<ReferenceInput>`. This also applies to other reference inputs like [`<ReferenceArrayInput>`](./ReferenceArrayInput.md).
118+
119+
## Validation
120+
121+
`<ReferenceInput>` doesn't accept a `validate` prop. Put validation on the child input instead (`<AutocompleteInput>`, `<SelectInput>`, `<RadioButtonGroupInput>`, etc.).
122+
123+
```jsx
124+
import { ReferenceInput, SelectInput, required } from 'react-admin';
125+
126+
<ReferenceInput source="company_id" reference="companies">
127+
<SelectInput validate={required()} />
128+
</ReferenceInput>
129+
```
118130

119131
## `children`
120132

docs/SelectArrayInput.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ If you need to *fetch* the options from another resource, you're actually editin
132132
</ReferenceArrayInput>
133133
```
134134

135+
**Tip**: If you need validation (e.g. `required()`), put the `validate` prop on the child `<SelectArrayInput>`. `<ReferenceArrayInput>` doesn't accept validation props.
136+
135137
You can also pass an *array of strings* for the choices:
136138

137139
```jsx

docs/SelectInput.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,19 +594,22 @@ const CompanyInput = () => {
594594
As this is a common task, react-admin provides a shortcut to do the same in a declarative way: [`<ReferenceInput>`](./ReferenceInput.md):
595595

596596
```jsx
597-
import { ReferenceInput, SelectInput } from 'react-admin';
597+
import { ReferenceInput, SelectInput, required } from 'react-admin';
598598

599599
const CompanyInput = () => (
600600
<ReferenceInput reference="companies" source="company_id">
601601
<SelectInput
602602
label="Company"
603603
source="company_id"
604604
optionText="name"
605+
validate={required()}
605606
/>
606607
</ReferenceInput>
607608
);
608609
```
609610

611+
**Tip**: When the input needs validation (e.g. `required()`), add the `validate` prop to the child `<SelectInput>`. `<ReferenceInput>` doesn't accept validation props.
612+
610613
`<ReferenceInput>` is a headless component that:
611614

612615
- fetches a list of records with `dataProvider.getList()` and `dataProvider.getOne()`, using the `reference` prop for the resource,

0 commit comments

Comments
 (0)