Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Forms): make it possible to reuse and extend internal validators #3908

Merged
merged 9 commits into from
Sep 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ All properties are optional and can be used as needed. These properties can be p
- `path` the JSON pointer that defines the entry name/key in the data structure.
- `itemPath` similar to `path`, but is used when run inside the [Iterate](/uilib/extensions/forms/Iterate/) context.

**Validation**
#### Validation

- `required` if true, it will call `validateRequired` for validation.
- `schema` or `pattern` for JSON schema validation powered by [ajv](https://ajv.js.org/).
Expand All @@ -89,7 +89,39 @@ All properties are optional and can be used as needed. These properties can be p
- `validateUnchanged` in order to validate without a change and blur event. Used for rare cases.
- `continuousValidation` in order to validate without a focus event beforehand. Used for rare cases.

**Error**
#### Validators

- `exportValidators` object with your validators you want to export. More info down below.

For more advanced use cases, you can export your custom Field validators with `exportValidators`. They are then available (as `validators` in object of the second validator parameter) to be used in the validator.
tujoworker marked this conversation as resolved.
Show resolved Hide resolved

When an array is returned from the validator, it will be used to only call these validators (in the order they are returned).
If no array is returned, the internal validator will be called in addition.

langz marked this conversation as resolved.
Show resolved Hide resolved
```tsx
const MyField = (props) => {
const myInternalValidator = useCallback(() => {
if (value === 'fail now') {
return new Error('Internal validation error')
}
}, [])
return (
<Field.String exportValidators={{ myInternalValidator }} {...props} />
)
}

const myValidator = (value, { validators: { myInternalValidator } }) => {
if (value === 'fail') {
return new Error('My error')
}

return [myInternalValidator] // optional
}

render(<MyField onBlurValidator={myValidator} />)
langz marked this conversation as resolved.
Show resolved Hide resolved
```

#### Error

- `error` object like `FormError` that includes the string to display or an object with the key `validationRule`. More info down below.
- `errorMessages` object with your custom messages, where each key represents a `validationRule`. More info down below.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ export const dataValueProperties: PropertiesTableProps = {
status: 'optional',
},
validator: {
doc: 'Custom validator function that is triggered on every change done by the user. The function can be either asynchronous or synchronous. The first parameter is the value, and the second parameter returns an object containing { errorMessages, connectWithPath }.',
doc: 'Custom validator function that is triggered on every change done by the user. The function can be either asynchronous or synchronous. The first parameter is the value, and the second parameter returns an object containing { errorMessages, connectWithPath, validators }.',
type: 'function',
status: 'optional',
},
onBlurValidator: {
doc: 'Custom validator function that is triggered when the user leaves a field (e.g., blurring a text input or closing a dropdown). The function can be either asynchronous or synchronous. The first parameter is the value, and the second parameter returns an object containing { errorMessages, connectWithPath }.',
doc: 'Custom validator function that is triggered when the user leaves a field (e.g., blurring a text input or closing a dropdown). The function can be either asynchronous or synchronous. The first parameter is the value, and the second parameter returns an object containing { errorMessages, connectWithPath, validators }.',
type: 'function',
status: 'optional',
},
Expand Down
Loading
Loading