|
| 1 | +import DocPage from '@docs/doc-page'; |
| 2 | + |
| 3 | +<DocPage> |
| 4 | + |
| 5 | +# Validator warnings |
| 6 | + |
| 7 | +If you don't want to block the form from submitting when a validator fails, you can convert the validator into a warning validator. |
| 8 | + |
| 9 | +## 1. Enable warnings |
| 10 | + |
| 11 | +In the field, set `useWarnings` to `true`. This feature can impact performace, so it was made to be opt-in. Please, do not change this value run-time as it could introduce bugs because of React hooks ordering. |
| 12 | + |
| 13 | +```jsx |
| 14 | +{ |
| 15 | + component: 'text-field', |
| 16 | + useWarnings: true, |
| 17 | + ... |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +## 2. Return warning |
| 22 | + |
| 23 | +### Object validator |
| 24 | + |
| 25 | +When you want to return an object validator as a function, just add `warning: true` to its configuration. |
| 26 | + |
| 27 | +```jsx |
| 28 | +{ |
| 29 | + ..., |
| 30 | + validate: [{ type: 'required', warning: true }] |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Function validator |
| 35 | + |
| 36 | +When using a function as the validator, the function has to return an object with the following format: |
| 37 | + |
| 38 | +``` |
| 39 | +{ |
| 40 | + warning: true, |
| 41 | + error: 'some message' |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## 3. Get warning |
| 46 | + |
| 47 | +A warning is stored in `meta.warning`. This value is specific for each field and fields with the same name do not share this. Also, this value is not reachable via `resolveProps` as it's computed just after the `resolveProps` function. |
| 48 | + |
| 49 | +```jsx |
| 50 | +const TextFieldWarning = (props) => { |
| 51 | + const { input, meta, ...rest } = useFieldApi(props); |
| 52 | + return ( |
| 53 | + <div> |
| 54 | + <input {...input} {...rest} /> |
| 55 | + {meta.warning && <div id="warning">{meta.warning}</div>} |
| 56 | + </div> |
| 57 | + ); |
| 58 | +}; |
| 59 | +``` |
| 60 | + |
| 61 | +### Convert to error |
| 62 | + |
| 63 | +By default, Data Driven Forms mappers are not supporting showing warnings. However you can convert the warning into an error - set `convertWarningToError` to `true`. This will replace `meta.error` with `meta.warning`, but `valid` etc. will be unchanged. |
| 64 | + |
| 65 | +```jsx |
| 66 | +{ |
| 67 | + name: 'warning-as-error', |
| 68 | + component: 'text-field', |
| 69 | + useWarnings: true, |
| 70 | + convertWarningToError: true, |
| 71 | + validate: [{type: 'required', warning: true}] |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +</DocPage> |
0 commit comments