Skip to content
Open
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
48 changes: 34 additions & 14 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1735,17 +1735,28 @@ export class FieldApi<

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (field.state.meta.errorMap?.[errorMapKey] !== newErrorValue) {
field.setMeta((prev) => ({
...prev,
errorMap: {
field.setMeta((prev) => {
const nextErrorMap = {
...prev.errorMap,
[errorMapKey]: newErrorValue,
},
errorSourceMap: {
...prev.errorSourceMap,
[errorMapKey]: newSource,
},
}))
}
// Clear stale onMount error when a later validator confirms the field is valid
if (
!newErrorValue &&
errorMapKey !== 'onMount' &&
prev.errorMap.onMount
) {
nextErrorMap.onMount = undefined
}
return {
...prev,
errorMap: nextErrorMap,
errorSourceMap: {
...prev.errorSourceMap,
[errorMapKey]: newSource,
},
}
})
Comment on lines +1738 to +1759
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Clear errorSourceMap.onMount to maintain consistency with errorMap.onMount.

When clearing errorMap.onMount, the corresponding errorSourceMap.onMount entry should also be cleared to keep the two maps in sync. The existing submit error clearing logic (lines 1787-1797) clears both maps, establishing this pattern.

Additionally, use optional chaining (prev.errorMap?.onMount) at line 1747 for defensive consistency with line 1737.

🔧 Proposed fix to clear errorSourceMap.onMount
         field.setMeta((prev) => {
           const nextErrorMap = {
             ...prev.errorMap,
             [errorMapKey]: newErrorValue,
           }
+          const nextErrorSourceMap = {
+            ...prev.errorSourceMap,
+            [errorMapKey]: newSource,
+          }
           // Clear stale onMount error when a later validator confirms the field is valid
           if (
             !newErrorValue &&
             errorMapKey !== 'onMount' &&
-            prev.errorMap.onMount
+            prev.errorMap?.onMount
           ) {
             nextErrorMap.onMount = undefined
+            nextErrorSourceMap.onMount = undefined
           }
           return {
             ...prev,
             errorMap: nextErrorMap,
-            errorSourceMap: {
-              ...prev.errorSourceMap,
-              [errorMapKey]: newSource,
-            },
+            errorSourceMap: nextErrorSourceMap,
           }
         })
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/form-core/src/FieldApi.ts` around lines 1738 - 1759, In
field.setMeta's updater (the block handling nextErrorMap), when you detect
clearing a stale onMount error (the if that checks !newErrorValue && errorMapKey
!== 'onMount' && prev.errorMap.onMount), also clear the corresponding entry in
errorSourceMap (set nextErrorSourceMap.onMount = undefined or ensure the
returned errorSourceMap has onMount removed) so errorMap and errorSourceMap stay
in sync; additionally change the check to use optional chaining
(prev.errorMap?.onMount) for defensive access. Reference: the field.setMeta
updater, variables errorMapKey, newErrorValue, newSource, and errorSourceMap in
this diff.

}
if (newErrorValue) {
hasErrored = true
Expand Down Expand Up @@ -1934,13 +1945,22 @@ export class FieldApi<
}

field.setMeta((prev) => {
const nextErrorMap = {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
...prev?.errorMap,
[errorMapKey]: newErrorValue,
}
// Clear stale onMount error when a later validator confirms the field is valid
if (
!newErrorValue &&
errorMapKey !== 'onMount' &&
prev.errorMap.onMount
) {
nextErrorMap.onMount = undefined
}
return {
...prev,
errorMap: {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
...prev?.errorMap,
[errorMapKey]: newErrorValue,
},
errorMap: nextErrorMap,
errorSourceMap: {
...prev.errorSourceMap,
[errorMapKey]: newSource,
Expand Down