|
1 | 1 | import Grid from '@material-ui/core/Grid' |
2 | 2 | import RawComponent from '@docs/raw-component'; |
| 3 | +import RouterLink from 'next/link'; |
| 4 | +import Link from '@material-ui/core/Link'; |
3 | 5 |
|
4 | 6 | import ListOfContents from '../../src/helpers/list-of-contents'; |
5 | 7 |
|
@@ -56,6 +58,57 @@ Validator inputs and results are being cached so you will get immediate feedback |
56 | 58 | 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) |
57 | 59 | (or any other implementation of debouncing.) |
58 | 60 |
|
| 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 | + |
59 | 112 | # ValidateOnMount pf3 only |
60 | 113 |
|
61 | 114 | By providing `validateOnMount` the validation will be triggered immediately after mounting of the component. |
|
0 commit comments