Skip to content

Commit

Permalink
feat(form-core): Add ability addErrors during submission
Browse files Browse the repository at this point in the history
  • Loading branch information
gutentag2012 committed Apr 6, 2024
1 parent ba4366f commit ff856ed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/reference/core/Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,17 @@ interface ValidatorAsyncOptions extends ValidatorOptions {
|--------------------|--------------------------------------------------------------|-------------|
| `debounceMs` | The debounce time in milliseconds. | `undefined` |
| `accumulateErrors` | Accumulates the errors instead of stopping at the first one. | `false` |

## ErrorTransformers

This is a collection of functions, that can be used to transform the error messages form a schema validation library to a format that is used by this library.

```ts
interface ErrorTransformers {
zod: (issues: ZodIssue[]) => Record<string, string>
}
```

::: info
Currently only `zod` is supported.
:::
9 changes: 9 additions & 0 deletions packages/form-core/src/FormLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export type FormLogicOptions<
/**
* Callback for when the form is submitted
* @param data The data at the time of submission
* @param addErrors A function to add errors to the form
*/
onSubmit?: (
data: TData,
Expand Down Expand Up @@ -536,6 +537,14 @@ export class FormLogic<
try {
await currentOptions.onSubmit(this._jsonData.peek(), (errors) => {
for (const [path, error] of Object.entries(errors)) {
if(!path) {
this.setErrors({
async: error,
asyncErrorEvent: 'server',
})
continue
}

const field = this.getFieldForPath(path as Paths<TData>)
if (!field) {
continue
Expand Down

0 comments on commit ff856ed

Please # to comment.