Skip to content

Commit 01b24a9

Browse files
jiji-hoon96autofix-ci[bot]LeCarbonator
authored
fix: sync validation errors for delayed-mounted fields (#1691)
* test: field mount bug test * feat: implement form error persistence for delayed-mounted fields * refactor: rename hidden field mount test file to use .test-d.ts extension * ci: apply automated fixes and generate docs * fix: ensure field errors are properly cleared when removing form fields * ci: apply automated fixes and generate docs * refactor: simplify field error management by removing _allFieldErrors state * refactor: replace setTimeout with vi.useFakeTimers and sleep utility in field tests * fix: handle undefined field metadata gracefully in form validation * ci: apply automated fixes and generate docs * fix: add optional chaining for fieldMeta after upstream merge * refactor: simplify form error handling logic * ci: apply automated fixes and generate docs * Add changeset * chore: fix wrong merge resolution * ci: apply automated fixes and generate docs --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: LeCarbonator <[email protected]>
1 parent 36fa503 commit 01b24a9

File tree

4 files changed

+403
-16
lines changed

4 files changed

+403
-16
lines changed

.changeset/ninety-trainers-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/form-core': patch
3+
---
4+
5+
Ensure dynamically rendered fields receive form validation errors

packages/form-core/src/FieldApi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,7 @@ export class FieldApi<
12571257
info.instance = this as never
12581258

12591259
this.update(this.options as never)
1260+
12601261
const { onMount } = this.options.validators || {}
12611262

12621263
if (onMount) {

packages/form-core/src/FormApi.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,16 +1671,20 @@ export class FormApi<
16711671

16721672
const errorMapKey = getErrorMapKey(validateObj.cause)
16731673

1674-
for (const field of Object.keys(
1675-
this.state.fieldMeta,
1676-
) as DeepKeys<TFormData>[]) {
1677-
if (this.baseStore.state.fieldMetaBase[field] === undefined) {
1674+
const allFieldsToProcess = new Set([
1675+
...Object.keys(this.state.fieldMeta),
1676+
...Object.keys(fieldErrors || {}),
1677+
] as DeepKeys<TFormData>[])
1678+
1679+
for (const field of allFieldsToProcess) {
1680+
if (
1681+
this.baseStore.state.fieldMetaBase[field] === undefined &&
1682+
!fieldErrors?.[field]
1683+
) {
16781684
continue
16791685
}
16801686

1681-
const fieldMeta = this.getFieldMeta(field)
1682-
if (!fieldMeta) continue
1683-
1687+
const fieldMeta = this.getFieldMeta(field) ?? defaultFieldMeta
16841688
const {
16851689
errorMap: currentErrorMap,
16861690
errorSourceMap: currentErrorMapSource,
@@ -1692,10 +1696,8 @@ export class FormApi<
16921696
determineFormLevelErrorSourceAndValue({
16931697
newFormValidatorError,
16941698
isPreviousErrorFromFormValidator:
1695-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1696-
currentErrorMapSource?.[errorMapKey] === 'form',
1697-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1698-
previousErrorValue: currentErrorMap?.[errorMapKey],
1699+
currentErrorMapSource[errorMapKey] === 'form',
1700+
previousErrorValue: currentErrorMap[errorMapKey],
16991701
})
17001702

17011703
if (newSource === 'form') {
@@ -1705,11 +1707,8 @@ export class FormApi<
17051707
}
17061708
}
17071709

1708-
if (
1709-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1710-
currentErrorMap?.[errorMapKey] !== newErrorValue
1711-
) {
1712-
this.setFieldMeta(field, (prev) => ({
1710+
if (currentErrorMap[errorMapKey] !== newErrorValue) {
1711+
this.setFieldMeta(field, (prev = defaultFieldMeta) => ({
17131712
...prev,
17141713
errorMap: {
17151714
...prev.errorMap,

0 commit comments

Comments
 (0)