Skip to content

Commit 404dd56

Browse files
authored
Merge pull request #1027 from rvsia/betterValidateDocs
Better validate docs
2 parents d455cf3 + 691f4ed commit 404dd56

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

packages/react-renderer-demo/src/components/navigation/schemas/schema.schema.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const schemaNav = [
2+
{
3+
component: 'introduction',
4+
linkText: 'Introduction'
5+
},
26
{
37
subHeader: true,
48
title: 'FieldProperties',
59
noRoute: true
610
},
7-
{
8-
component: 'introduction',
9-
linkText: 'Introduction'
10-
},
1111
{
1212
component: 'clear-on-unmount',
1313
linkText: 'Clear on unmount'
@@ -69,6 +69,10 @@ const schemaNav = [
6969
component: 'custom-validator',
7070
linkText: 'Custom validator'
7171
},
72+
{
73+
component: 'custom-validator-message',
74+
linkText: 'Custom message'
75+
},
7276
{
7377
component: 'overwriting-default-message',
7478
linkText: 'Overwriting default message'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import FormRenderer from '@data-driven-forms/react-form-renderer/form-renderer';
3+
import componentTypes from '@data-driven-forms/react-form-renderer/component-types';
4+
import FormTemplate from '@data-driven-forms/mui-component-mapper/form-template';
5+
import TextField from '@data-driven-forms/mui-component-mapper/text-field';
6+
import validatorTypes from '@data-driven-forms/react-form-renderer/validator-types';
7+
8+
const componentMapper = {
9+
[componentTypes.TEXT_FIELD]: TextField
10+
};
11+
const schema = {
12+
fields: [
13+
{
14+
component: componentTypes.TEXT_FIELD,
15+
name: 'custom-validator',
16+
validate: [{ type: validatorTypes.REQUIRED, message: 'This field is required' }],
17+
validateOnMount: true
18+
}
19+
]
20+
};
21+
22+
const CustomMessage = () => <FormRenderer FormTemplate={FormTemplate} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />;
23+
24+
export default CustomMessage;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import DocPage from '@docs/doc-page';
2+
import CodeExample from '@docs/code-example';
3+
4+
<DocPage>
5+
6+
# Custom validator message
7+
8+
You can overwrite the messages [globally](/schema/overwriting-default-message) or specifically in each validator object via setting `message` attribute.
9+
10+
```jsx
11+
{
12+
...,
13+
validate: [{type: 'required', message: 'This field is required'}]
14+
}
15+
```
16+
17+
## Example
18+
19+
<CodeExample mode="preview" source="components/validators/custom-message" />
20+
21+
</DocPage>

packages/react-renderer-demo/src/pages/schema/introduction.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,20 @@ A function allowing to compute field properties from the current state of the fi
186186
```jsx
187187
{
188188
...,
189-
validate: [{type: 'required'}]
189+
validate: [{type: 'required', message: 'This field is required'}]
190190
}
191191
```
192192

193193
Type is one of our provided validators: [required](/schema/required-validator), [length](/schema/length-validator), [URL](/schema/url-validator), [pattern](/schema/pattern-validator), [number value](/schema/number-value-validator). You can use [validatorTypes](/schema/constants#validatortypes) to prevent typos.
194194

195195
Or you can implement your own via creating a [validator mapper](/mappers/validator-mapper).
196196

197+
**Message**
198+
199+
All provided validators let you change the message via [message attribute](/schema/custom-validator-message) or you can set them [globally](/schema/overwriting-default-message).
200+
201+
By default, each validator provides a default message in English.
202+
197203
**function**
198204

199205
You can pass a [custom function](/schema/custom-validator):

0 commit comments

Comments
 (0)