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
28 changes: 28 additions & 0 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,34 @@ export class FieldApi<
info.instance = this as never

this.update(this.options as never)

const allFieldErrors = this.form.state._allFieldErrors
if (allFieldErrors?.[this.name]) {
const existingErrorMap = allFieldErrors[this.name]
if (existingErrorMap) {
this.setMeta(
(prev) =>
({
...prev,
errorMap: {
...prev.errorMap,
...existingErrorMap,
},
errorSourceMap: {
...prev.errorSourceMap,
...Object.keys(existingErrorMap).reduce(
(acc, key) => {
acc[key as keyof typeof acc] = 'form'
return acc
},
{} as Record<string, 'form'>,
),
},
}) as never,
)
}
}

const { onMount } = this.options.validators || {}

if (onMount) {
Expand Down
43 changes: 40 additions & 3 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,11 @@ export type BaseFormState<
* @private, used to force a re-evaluation of the form state when options change
*/
_force_re_eval?: boolean
/**
* @private, stores field errors for all fields (including unmounted ones)
* This allows delayed-mounted fields to sync with existing validation errors
*/
_allFieldErrors?: Partial<Record<DeepKeys<TFormData>, ValidationErrorMap>>
}

export type DerivedFormState<
Expand Down Expand Up @@ -1532,9 +1537,35 @@ export class FormApi<

const errorMapKey = getErrorMapKey(validateObj.cause)

for (const field of Object.keys(
this.state.fieldMeta,
) as DeepKeys<TFormData>[]) {
if (fieldErrors) {
const allFieldErrors: Partial<
Record<DeepKeys<TFormData>, ValidationErrorMap>
> = this.state._allFieldErrors || {}
for (const [fieldName, fieldError] of Object.entries(fieldErrors)) {
if (fieldError) {
const typedFieldName = fieldName as DeepKeys<TFormData>
allFieldErrors[typedFieldName] = {
...allFieldErrors[typedFieldName],
[errorMapKey]: fieldError,
}
}
}
this.baseStore.setState((prev) => ({
...prev,
_allFieldErrors: allFieldErrors,
}))
}

const allFieldsToProcess = new Set([
...Object.keys(this.state.fieldMeta),
...Object.keys(fieldErrors || {}),
] as DeepKeys<TFormData>[])

for (const field of allFieldsToProcess) {
if (fieldErrors?.[field] && !this.fieldInfo[field]) {
this.getFieldInfo(field)
}

const fieldMeta = this.getFieldMeta(field)
if (!fieldMeta) continue

Expand Down Expand Up @@ -2095,6 +2126,12 @@ export class FormApi<
newState.values = deleteBy(newState.values, f)
delete this.fieldInfo[f as never]
delete newState.fieldMetaBase[f as never]

if (newState._allFieldErrors?.[f as never]) {
const newAllFieldErrors = { ...newState._allFieldErrors }
delete newAllFieldErrors[f as never]
newState._allFieldErrors = newAllFieldErrors
}
})

return newState
Expand Down
Loading
Loading