Skip to content

Commit 4f39347

Browse files
crutchcornBalastrong
authored andcommitted
feat: Field validation from form schema (#925)
* 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 <[email protected]>
1 parent 22ece11 commit 4f39347

File tree

22 files changed

+942
-151
lines changed

22 files changed

+942
-151
lines changed

docs/framework/angular/guides/validation.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,38 @@ export class AppComponent {
469469
}
470470
```
471471

472+
### Form Level Adapter Validation
473+
474+
You can also use the adapter at the form level:
475+
476+
```typescript
477+
import { zodValidator } from '@tanstack/zod-form-adapter'
478+
import { z } from 'zod'
479+
480+
// ...
481+
482+
const form = injectForm({
483+
validatorAdapter: zodValidator(),
484+
validators: {
485+
onChange: z.object({
486+
age: z.number().gte(13, 'You must be 13 to make an account'),
487+
}),
488+
},
489+
})
490+
```
491+
492+
If you use the adapter at the form level, it will pass the validation to the fields of the same name.
493+
494+
This means that:
495+
496+
```html
497+
<ng-container [tanstackField]="form" name="age" #age="field">
498+
<!-- ... -->
499+
</ng-container>
500+
```
501+
502+
Will still display the error message from the form-level validation.
503+
472504
## Preventing invalid forms from being submitted
473505

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

docs/framework/react/guides/validation.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,42 @@ These adapters also support async operations using the proper property names:
477477
/>
478478
```
479479
480+
### Form Level Adapter Validation
481+
482+
You can also use the adapter at the form level:
483+
484+
```tsx
485+
import { zodValidator } from '@tanstack/zod-form-adapter'
486+
import { z } from 'zod'
487+
488+
// ...
489+
490+
const form = useForm({
491+
validatorAdapter: zodValidator(),
492+
validators: {
493+
onChange: z.object({
494+
age: z.number().gte(13, 'You must be 13 to make an account'),
495+
}),
496+
},
497+
})
498+
```
499+
500+
If you use the adapter at the form level, it will pass the validation to the fields of the same name.
501+
502+
This means that:
503+
504+
```tsx
505+
506+
<form.Field
507+
name="age"
508+
children={(field) => {
509+
return <>{/* ... */}</>
510+
}}
511+
/>
512+
```
513+
514+
Will still display the error message from the form-level validation.
515+
480516
## Preventing invalid forms from being submitted
481517
482518
The `onChange`, `onBlur` etc... callbacks are also run when the form is submitted and the submission is blocked if the form is invalid.

docs/framework/solid/guides/validation.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,44 @@ These adapters also support async operations using the proper property names:
376376
/>
377377
```
378378

379+
### Form Level Adapter Validation
380+
381+
382+
You can also use the adapter at the form level:
383+
384+
```tsx
385+
import { zodValidator } from '@tanstack/zod-form-adapter'
386+
import { z } from 'zod'
387+
388+
// ...
389+
390+
const form = createForm(() => ({
391+
validatorAdapter: zodValidator(),
392+
validators: {
393+
onChange: z.object({
394+
age: z.number().gte(13, 'You must be 13 to make an account'),
395+
}),
396+
},
397+
}))
398+
```
399+
400+
If you use the adapter at the form level, it will pass the validation to the fields of the same name.
401+
402+
This means that:
403+
404+
```tsx
405+
406+
<form.Field
407+
name="age"
408+
children={(field) => {
409+
return <>{/* ... */}</>
410+
}}
411+
/>
412+
```
413+
414+
Will still display the error message from the form-level validation.
415+
416+
379417
## Preventing invalid forms from being submitted
380418

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

0 commit comments

Comments
 (0)