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] Field validation from form schema #925

Merged
merged 18 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading