Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/happy-pillows-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/form-core': patch
---

run validation for fields without instances
13 changes: 12 additions & 1 deletion packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,18 @@ export class FormApi<
) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const fieldInstance = this.fieldInfo[field]?.instance
if (!fieldInstance) return []

if (!fieldInstance) {
const { hasErrored } = this.validateSync(cause)

if (hasErrored && !this.options.asyncAlways) {
return this.getFieldMeta(field)?.errors ?? []
}

return this.validateAsync(cause).then(() => {
return this.getFieldMeta(field)?.errors ?? []
})
}

// If the field is not touched (same logic as in validateAllFields)
if (!fieldInstance.state.meta.isTouched) {
Expand Down
25 changes: 25 additions & 0 deletions packages/form-core/tests/standardSchemaValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,31 @@ describe('standard schema validator', () => {
])
})

it('should handle form-level field errors for fields without a mounted FieldApi instance', () => {
const form = new FormApi({
defaultValues: {
email: '',
},
validators: {
onChange: z.object({
email: z.string().email('email must be an email address'),
}),
},
})

form.mount()

form.setFieldValue('email', 'not-an-email')

expect(form.state.errors).toMatchObject([
{ email: [{ message: 'email must be an email address' }] },
])

form.setFieldValue('email', 'test@example.com')

expect(form.state.errors).toEqual([])
})

it('should support standard schema async validation with zod', async () => {
vi.useFakeTimers()

Expand Down
Loading