Skip to content

Commit

Permalink
[feat] Field validation from form schema (#925)
Browse files Browse the repository at this point in the history
* feat: add ability to pass errors to specific fields from Zod

* chore: code cleanup

* chore: fix various issues

* chore: more TSC and lib fixes

* ci: apply automated fixes

* feat: add Yup support to schema valdiation

* chore: rename contexts from code review

* ci: apply automated fixes

* feat: support form validaton in Valibot

* chore: fix tests for createServerValidate

* docs: add docs for validation logic

* feat: apply transformError to all fields

* fix: set transformed errors on nested fields

* feat: spread zod errors into arrays

* feat: spread yup errors into fields, nested and arrays

* feat: spread valibot errors into fields, nested and arrays

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Leonardo Montini <lion.48m@gmail.com>
  • Loading branch information
3 people committed Sep 18, 2024
1 parent 22ece11 commit 2785711
Show file tree
Hide file tree
Showing 22 changed files with 942 additions and 151 deletions.
32 changes: 32 additions & 0 deletions docs/framework/angular/guides/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,38 @@ export class AppComponent {
}
```

### Form Level Adapter Validation

You can also use the adapter at the form level:

```typescript
import { zodValidator } from '@tanstack/zod-form-adapter'
import { z } from 'zod'

// ...

const form = injectForm({
validatorAdapter: zodValidator(),
validators: {
onChange: z.object({
age: z.number().gte(13, 'You must be 13 to make an account'),
}),
},
})
```

If you use the adapter at the form level, it will pass the validation to the fields of the same name.

This means that:

```html
<ng-container [tanstackField]="form" name="age" #age="field">
<!-- ... -->
</ng-container>
```

Will still display the error message from the form-level validation.

## Preventing invalid forms from being submitted

The `onChange`, `onBlur` etc... callbacks are also run when the form is submitted and the submission is blocked if the form is invalid.
Expand Down
36 changes: 36 additions & 0 deletions docs/framework/react/guides/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,42 @@ These adapters also support async operations using the proper property names:
/>
```
### Form Level Adapter Validation
You can also use the adapter at the form level:
```tsx
import { zodValidator } from '@tanstack/zod-form-adapter'
import { z } from 'zod'
// ...
const form = useForm({
validatorAdapter: zodValidator(),
validators: {
onChange: z.object({
age: z.number().gte(13, 'You must be 13 to make an account'),
}),
},
})
```
If you use the adapter at the form level, it will pass the validation to the fields of the same name.
This means that:
```tsx
<form.Field
name="age"
children={(field) => {
return <>{/* ... */}</>
}}
/>
```
Will still display the error message from the form-level validation.
## Preventing invalid forms from being submitted
The `onChange`, `onBlur` etc... callbacks are also run when the form is submitted and the submission is blocked if the form is invalid.
Expand Down
38 changes: 38 additions & 0 deletions docs/framework/solid/guides/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,44 @@ These adapters also support async operations using the proper property names:
/>
```

### Form Level Adapter Validation


You can also use the adapter at the form level:

```tsx
import { zodValidator } from '@tanstack/zod-form-adapter'
import { z } from 'zod'

// ...

const form = createForm(() => ({
validatorAdapter: zodValidator(),
validators: {
onChange: z.object({
age: z.number().gte(13, 'You must be 13 to make an account'),
}),
},
}))
```

If you use the adapter at the form level, it will pass the validation to the fields of the same name.

This means that:

```tsx

<form.Field
name="age"
children={(field) => {
return <>{/* ... */}</>
}}
/>
```

Will still display the error message from the form-level validation.


## Preventing invalid forms from being submitted

The `onChange`, `onBlur` etc... callbacks are also run when the form is submitted and the submission is blocked if the form is invalid.
Expand Down
Loading

0 comments on commit 2785711

Please sign in to comment.