setFieldValue clears errorMap.onMount but leaves errorSourceMap.onMount stale

Open Beginner friendly
#2,294 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
frontend

Research direction

Start in FormApi.ts at setFieldValue, where the field metadata clears errorMap.onMount. Reproduce the stale-state case with the FormApi and FieldApi setup described in the issue, then verify that changing the field value clears both errorMap.onMount and errorSourceMap.onMount.

Written by the indexing model from the issue text.

Description

v1 v2: needs investigation
Describe the bug

FormApi.setFieldValue clears a field's stale onMount error when the field's own value changes, but only clears it from errorMap, not from errorSourceMap, so the two maps can end up out of sync.

// FormApi.ts, setFieldValue
this.setFieldMeta(field, (prev) => ({
  ...prev,
  isTouched: true,
  isDirty: true,
  errorMap: {
    ...prev?.errorMap,
    onMount: undefined,
  },
}))

errorSourceMap.onMount is never touched here. Both maps are surfaced to consumers directly, for example useField in react-form exposes errorSourceMap: reactiveMetaErrorSourceMap as part of field.state.meta alongside errorMap, so this isn't just internal bookkeeping, application code can read meta.errorSourceMap.onMount directly.

After a value change clears a field's onMount error, meta.errorMap.onMount correctly becomes undefined, but meta.errorSourceMap.onMount keeps whatever source ('form' or 'field') was last recorded there. Code that checks errorSourceMap.onMount to decide something (which validator layer an error came from, for instance) would see a source for an error that no longer exists in errorMap.

Steps to reproduce
const form = new FormApi({
  defaultValues: { name: '' },
  validators: {
    onMount: () => 'Required',
  },
})
const field = new FieldApi({ form, name: 'name', validators: { onMount: () => 'Required' } })
form.mount()
field.mount()

console.log(field.state.meta.errorMap.onMount) // 'Required'
console.log(field.state.meta.errorSourceMap.onMount) // 'field'

field.setValue('something')

console.log(field.state.meta.errorMap.onMount) // undefined, cleared
console.log(field.state.meta.errorSourceMap.onMount) // still 'field', not cleared
Expected behavior

errorSourceMap.onMount should be cleared alongside errorMap.onMount in setFieldValue, the same way #2244 (currently open) clears both together for the linked-field revalidation case.

Actual behavior

errorSourceMap.onMount is left stale after setFieldValue clears the corresponding errorMap.onMount.

Dominant language
TypeScript
Stars
6.7k
Forks
682
Avg merge
5d 18h
Merged PRs (30d)
7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TanStack/form

All issues in TanStack/form

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.