Skip to content

Commit

Permalink
fix: spread field errors on form onMount validator (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
Balastrong authored Dec 16, 2024
1 parent 91eb9ab commit ab9dd76
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 41 deletions.
40 changes: 20 additions & 20 deletions docs/reference/classes/formapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ deleteField<TField>(field): void
#### Defined in
[packages/form-core/src/FormApi.ts:1135](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1135)
[packages/form-core/src/FormApi.ts:1122](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1122)
***
Expand Down Expand Up @@ -153,7 +153,7 @@ Gets the field info of the specified field.
#### Defined in
[packages/form-core/src/FormApi.ts:1046](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1046)
[packages/form-core/src/FormApi.ts:1033](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1033)
***
Expand Down Expand Up @@ -181,7 +181,7 @@ Gets the metadata of the specified field.
#### Defined in
[packages/form-core/src/FormApi.ts:1037](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1037)
[packages/form-core/src/FormApi.ts:1024](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1024)
***
Expand Down Expand Up @@ -209,7 +209,7 @@ Gets the value of the specified field.
#### Defined in
[packages/form-core/src/FormApi.ts:1030](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1030)
[packages/form-core/src/FormApi.ts:1017](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1017)
***
Expand All @@ -227,7 +227,7 @@ Handles the form submission, performs validation, and calls the appropriate onSu
#### Defined in
[packages/form-core/src/FormApi.ts:971](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L971)
[packages/form-core/src/FormApi.ts:958](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L958)
***
Expand Down Expand Up @@ -271,7 +271,7 @@ Inserts a value into an array field at the specified index, shifting the subsequ
#### Defined in
[packages/form-core/src/FormApi.ts:1167](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1167)
[packages/form-core/src/FormApi.ts:1154](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1154)
***
Expand Down Expand Up @@ -331,7 +331,7 @@ Moves the value at the first specified index to the second specified index withi
#### Defined in
[packages/form-core/src/FormApi.ts:1285](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1285)
[packages/form-core/src/FormApi.ts:1272](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1272)
***
Expand Down Expand Up @@ -370,7 +370,7 @@ Pushes a value into an array field.
#### Defined in
[packages/form-core/src/FormApi.ts:1149](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1149)
[packages/form-core/src/FormApi.ts:1136](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1136)
***
Expand Down Expand Up @@ -409,7 +409,7 @@ Removes a value from an array field at the specified index.
#### Defined in
[packages/form-core/src/FormApi.ts:1220](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1220)
[packages/form-core/src/FormApi.ts:1207](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1207)
***
Expand Down Expand Up @@ -453,7 +453,7 @@ Replaces a value into an array field at the specified index.
#### Defined in
[packages/form-core/src/FormApi.ts:1194](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1194)
[packages/form-core/src/FormApi.ts:1181](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1181)
***
Expand Down Expand Up @@ -488,7 +488,7 @@ Optional options to control the reset behavior.
#### Defined in
[packages/form-core/src/FormApi.ts:599](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L599)
[packages/form-core/src/FormApi.ts:586](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L586)
***
Expand All @@ -514,7 +514,7 @@ resetFieldMeta<TField>(fieldMeta): Record<TField, FieldMeta>
#### Defined in
[packages/form-core/src/FormApi.ts:1080](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1080)
[packages/form-core/src/FormApi.ts:1067](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1067)
***
Expand All @@ -538,7 +538,7 @@ Updates the form's errorMap
#### Defined in
[packages/form-core/src/FormApi.ts:1309](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1309)
[packages/form-core/src/FormApi.ts:1296](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1296)
***
Expand Down Expand Up @@ -570,7 +570,7 @@ Updates the metadata of the specified field.
#### Defined in
[packages/form-core/src/FormApi.ts:1065](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1065)
[packages/form-core/src/FormApi.ts:1052](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1052)
***
Expand Down Expand Up @@ -609,7 +609,7 @@ Sets the value of the specified field and optionally updates the touched state.
#### Defined in
[packages/form-core/src/FormApi.ts:1104](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1104)
[packages/form-core/src/FormApi.ts:1091](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1091)
***
Expand Down Expand Up @@ -653,7 +653,7 @@ Swaps the values at the specified indices within an array field.
#### Defined in
[packages/form-core/src/FormApi.ts:1259](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1259)
[packages/form-core/src/FormApi.ts:1246](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1246)
***
Expand All @@ -677,7 +677,7 @@ Updates the form options and form state.
#### Defined in
[packages/form-core/src/FormApi.ts:555](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L555)
[packages/form-core/src/FormApi.ts:542](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L542)
***
Expand All @@ -701,7 +701,7 @@ Validates form and all fields in using the correct handlers for a given validati
#### Defined in
[packages/form-core/src/FormApi.ts:625](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L625)
[packages/form-core/src/FormApi.ts:612](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L612)
***
Expand Down Expand Up @@ -740,7 +740,7 @@ Validates the children of a specified array in the form starting from a given in
#### Defined in
[packages/form-core/src/FormApi.ts:659](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L659)
[packages/form-core/src/FormApi.ts:646](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L646)
***
Expand Down Expand Up @@ -772,4 +772,4 @@ Validates a specified field in the form using the correct handlers for a given v
#### Defined in
[packages/form-core/src/FormApi.ts:698](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L698)
[packages/form-core/src/FormApi.ts:685](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L685)
6 changes: 3 additions & 3 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Store } from '@tanstack/store'
import { getAsyncValidatorArray, getBy, getSyncValidatorArray } from './utils'
import {
isStandardSchemaValidator,
standardSchemaValidator,
} from './standardSchemaValidator'
import type { StandardSchemaV1 } from './standardSchemaValidator'
import { getAsyncValidatorArray, getBy, getSyncValidatorArray } from './utils'
import type { FieldInfo, FieldsErrorMapFromValidator, FormApi } from './FormApi'
import type { StandardSchemaV1 } from './standardSchemaValidator'
import type {
UpdateMetaOptions,
ValidationCause,
Expand All @@ -14,8 +14,8 @@ import type {
ValidationSource,
Validator,
} from './types'
import type { AsyncValidator, SyncValidator, Updater } from './utils'
import type { DeepKeys, DeepValue, NoInfer } from './util-types'
import type { AsyncValidator, SyncValidator, Updater } from './utils'

/**
* @private
Expand Down
17 changes: 2 additions & 15 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,21 +532,8 @@ export class FormApi<
mount = () => {
const { onMount } = this.options.validators || {}
if (!onMount) return
const error = this.runValidator({
validate: onMount,
value: {
value: this.state.values,
formApi: this,
validationSource: 'form',
},
type: 'validate',
})
if (error) {
this.store.setState((prev) => ({
...prev,
errorMap: { ...prev.errorMap, onMount: error },
}))
}

this.validateSync('mount')
}

/**
Expand Down
15 changes: 12 additions & 3 deletions packages/form-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,24 @@ export function getSyncValidatorArray<T>(
cause: ValidationCause,
options: SyncValidatorArrayPartialOptions<T>,
): T extends FieldValidators<any, any>
? Array<SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit']>>
? Array<
SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit'] | T['onMount']>
>
: T extends FormValidators<any, any>
? Array<SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit']>>
? Array<
SyncValidator<
T['onChange'] | T['onBlur'] | T['onSubmit'] | T['onMount']
>
>
: never {
const { onChange, onBlur, onSubmit } = (options.validators || {}) as
const { onChange, onBlur, onSubmit, onMount } = (options.validators || {}) as
| FieldValidators<any, any>
| FormValidators<any, any>

const changeValidator = { cause: 'change', validate: onChange } as const
const blurValidator = { cause: 'blur', validate: onBlur } as const
const submitValidator = { cause: 'submit', validate: onSubmit } as const
const mountValidator = { cause: 'mount', validate: onMount } as const

// Allows us to clear onServer errors
const serverValidator = {
Expand All @@ -299,6 +306,8 @@ export function getSyncValidatorArray<T>(
} as const

switch (cause) {
case 'mount':
return [mountValidator] as never
case 'submit':
return [
changeValidator,
Expand Down
31 changes: 31 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2353,4 +2353,35 @@ describe('form api', () => {
expect(form.state.canSubmit).toBe(true)
expect(passconfirmField.state.meta.errors.length).toBe(0)
})

it("should set field errors from the form's onMount validator", async () => {
const form = new FormApi({
defaultValues: {
firstName: '',
},
validators: {
onMount: () => {
return {
form: 'something went wrong',
fields: {
firstName: 'first name is required',
},
}
},
},
})

const firstNameField = new FieldApi({
form,
name: 'firstName',
})

firstNameField.mount()
form.mount()

expect(form.state.errorMap.onMount).toBe('something went wrong')
expect(firstNameField.state.meta.errorMap.onMount).toBe(
'first name is required',
)
})
})

0 comments on commit ab9dd76

Please sign in to comment.