Skip to content
Source
Sign inSign up
TanStack/form

fix(form-core): establish Field-over-Form prioritization for isDefaultValue and resets #2006

Mergedkyu-rong(GitHub profile, opens in a new tab)merged 6 commits intomain fromrefs/pull/2006/headmerged by crutchcorn(GitHub profile, opens in a new tab)

Showing 4 changed files with 95 additions and 21 deletions (loading 1).

x
0 of 4 viewed, 4 left
5 additions and 0 deletions.changeset/gentle-jars-share.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/form-core': minor
+---
+​
+Introduced a **Prioritized Default System** that ensures consistency between field metadata and form reset behavior. This change prioritizes field-level default values over form-level defaults across `isDefaultValue` derivation, `form.reset()`, and `form.resetField()`. This ensures that field metadata accurately reflects the state the form would return to upon reset and prevents `undefined` from being incorrectly treated as a default when a value is explicitly specified.
Thread on lines 2 to 5
coderabbitai[bot](GitHub profile, opens in a new tab)commented

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Recheck semver level for this behavioral precedence change.

This changes user-visible semantics of isDefaultValue, reset(), and resetField(); consumers depending on previous dual-default behavior may break. Please confirm whether this should be a major bump (or feature-flagged), not minor.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.changeset/gentle-jars-share.md around lines 2 - 5, The changeset claims a
minor bump but introduces behavioral precedence altering user-visible semantics
(Prioritized Default System) affecting isDefaultValue, form.reset(), and
form.resetField(); re-evaluate and change the changeset to a major version bump
or gate the behavior behind a feature flag/opt-in, update the .changeset text to
reflect the decision, and ensure release notes explicitly document the
behavioral change and migration guidance for consumers relying on previous
dual-default behavior.
41 additions and 20 deletionspackages/form-core/src/FormApi.tsModified
View file
@@ -1132,16 +1132,12 @@
// As primitives, we don't need to aggressively persist the same referential value for performance reasons
const isFieldValid = !isNonEmptyArray(fieldErrors)
const isFieldPristine = !currBaseMeta.isDirty
- const isDefaultValue =
- evaluate(
- curFieldVal,
+ const isDefaultValue = evaluate(
+ curFieldVal,
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+ this.getFieldInfo(fieldName)?.instance?.options.defaultValue ??
getBy(this.options.defaultValues, fieldName),
- ) ||
- evaluate(
- curFieldVal,
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
- this.getFieldInfo(fieldName)?.instance?.options.defaultValue,
- )
+ )
Thread on lines 1135 to 1140
coderabbitai[bot](GitHub profile, opens in a new tab)commented

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

null field defaults lose precedence due to ??.

Using nullish coalescing here makes explicit defaultValue: null fall back to form defaults, which breaks Field-over-Form priority and can desynchronize reset/default metadata behavior.

Suggested fix
-const isDefaultValue = evaluate(
-  curFieldVal,
-  this.getFieldInfo(fieldName)?.instance?.options.defaultValue ??
-    getBy(this.options.defaultValues, fieldName),
-)
+const fieldDefaultValue =
+  this.getFieldInfo(fieldName).instance?.options.defaultValue
+const prioritizedDefaultValue =
+  fieldDefaultValue !== undefined
+    ? fieldDefaultValue
+    : getBy(this.options.defaultValues, fieldName)
+const isDefaultValue = evaluate(curFieldVal, prioritizedDefaultValue)
-const fieldDefault =
-  this.getFieldInfo(field).instance?.options.defaultValue
-const formDefault = getBy(this.options.defaultValues, field)
-const targetValue = fieldDefault ?? formDefault
+const fieldDefault =
+  this.getFieldInfo(field).instance?.options.defaultValue
+const formDefault = getBy(this.options.defaultValues, field)
+const targetValue = fieldDefault !== undefined ? fieldDefault : formDefault

Also applies to: 2601-2605

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/form-core/src/FormApi.ts` around lines 1135 - 1140, The code uses
nullish coalescing (??) so an explicit field default of null is treated as
missing and falls back to form defaults; update the logic in the evaluate call
to treat only undefined as "missing" (preserve null/false/0 as valid defaults).
Concretely, retrieve the field-specific default via
this.getFieldInfo(fieldName)?.instance?.options.defaultValue into a local (e.g.,
fieldDefault) and use fieldDefault !== undefined ? fieldDefault :
getBy(this.options.defaultValues, fieldName) when calling evaluate (change the
occurrence inside evaluate and the similar occurrences around the 2601-2605
area) so explicit null values retain precedence.