Skip to content

Commit 5b480b8

Browse files
committed
Add docs for validatorMapper
1 parent 40a7fbb commit 5b480b8

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

packages/react-renderer-demo/src/app/pages/renderer/renderer-api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Form Renderer provides a lot of customization via props.
3333
|initialValues|object|An object of fields names as keys and values as their values.||
3434
|subscription|object|You can pass your own [subscription](https://final-form.org/docs/react-final-form/types/FormProps#subscription), which will be added to default settings.|`{ pristine: true, submitting: true, valid: true }`|
3535
|<RouterLink href="/renderer/validators"><Link href="/renderer/validators">validate</Link></RouterLink>|func|A function which receives all form values and returns an object with errors.||
36+
|<RouterLink href="/renderer/validators#validatormapper"><Link href="/renderer/validators#validatormapper">validatorMapper</Link></RouterLink>|object|A mapper containing custom validators, it's automatically merged with the default one.||
3637

3738
# Schema
3839

packages/react-renderer-demo/src/app/pages/renderer/validators.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Grid from '@material-ui/core/Grid'
22
import RawComponent from '@docs/raw-component';
3+
import RouterLink from 'next/link';
4+
import Link from '@material-ui/core/Link';
35

46
import ListOfContents from '../../src/helpers/list-of-contents';
57

@@ -56,6 +58,57 @@ Validator inputs and results are being cached so you will get immediate feedback
5658
If you do not want to trigger the async validator after every stroke, you can use a debounce promise [library](https://github.com/slorber/awesome-debounce-promise)
5759
(or any other implementation of debouncing.)
5860

61+
# validatorMapper
62+
63+
If you need to expand default Data Driven Forms validator types, you can use <RouterLink href="/renderer/renderer-api#optionalprops"><Link href="/renderer/renderer-api#optionalprops">validatorMapper</Link></RouterLink>.
64+
65+
```jsx
66+
const customValidatorMapper = {
67+
custom: () => (value) => value > 6 ? 'Value is bigger than 6' : undefined
68+
}
69+
70+
const schema = {
71+
fields: [{
72+
name: 'name',
73+
component: 'text-field',
74+
validate: [{type: 'custom'}]
75+
}]
76+
}
77+
78+
<FormRenderer
79+
...
80+
schema={schema}
81+
validatorMapper={customValidatorMapper}
82+
/>
83+
84+
```
85+
86+
It is designed to return functions returning functions, so you can easily cached or debounce results.
87+
88+
The higher order function receives the whole validator object.
89+
90+
```jsx
91+
const customValidatorMapper = {
92+
custom: ({ threshold }) => (value) => value > threshold ? `Value is bigger than ${threshold}` : undefined
93+
}
94+
95+
const schema = {
96+
fields: [{
97+
name: 'name',
98+
component: 'text-field',
99+
validate: [{type: 'custom', threshold: 6}]
100+
}]
101+
}
102+
```
103+
104+
Also, each validator function receives value of the current field as the first argument and all form values as the second.
105+
106+
```jsx
107+
const validatorMapper = {
108+
[type]: (validatorSchema) => (value, allValues) => isValid ? undefined : 'error message'
109+
}
110+
```
111+
59112
# ValidateOnMount pf3 only
60113

61114
By providing `validateOnMount` the validation will be triggered immediately after mounting of the component.

0 commit comments

Comments
 (0)