fix(form-core): mark unrendered fields as touched on submit #2449
Openokxint(GitHub profile, opens in a new tab)wants to merge 2 commits into
main fromrefs/pull/2449/headopened - packages/form-core/src/FormApi.ts lines 2448 to 2451OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2435,6 +2434,18 @@2434+ FieldInfo<any>,2435+ ][]2436+ ).forEach(([fieldName, field]) => {2437+ if (field.instance) {2438+ // Rendered field: update through the live instance24352439 if (!field.instance.store.state.meta.isTouched) {2436- // Mark them as touched24372440 field.instance.setMeta((prev) => ({ ...prev, isTouched: true }))24382441 }2439- },2440- )2442+ } else {2443+ // Unrendered field (e.g. virtualized): update the store directly so2444+ // that any validation triggered by the submission attempt can still2445+ // surface errors on these fields when they are next rendered.2446+ const meta = this.getFieldMeta(fieldName) ?? defaultFieldMeta2447+ if (!meta.isTouched) {2448+ this.setFieldMeta(fieldName, (prev = defaultFieldMeta) => ({2449+ ...prev,2450+ isTouched: true,2451+ }))
- @@ -2440,0 +2444,21 @@2444+ } else {2445+ // Unrendered field (e.g. virtualized): collect for a single batch2446+ // store write to avoid O(n²) fieldMetaBase copies for large lists.2447+ const meta = this.getFieldMeta(fieldName) ?? defaultFieldMeta2448+ if (!meta.isTouched) {2449+ untouchedUnrendered.push(fieldName)2450+ }2451+ }2452+ })2453+2454+ if (untouchedUnrendered.length > 0) {2455+ this.baseStore.setState((prev) => {2456+ const fieldMetaBase = { ...prev.fieldMetaBase }2457+ for (const fieldName of untouchedUnrendered) {2458+ const existing =2459+ (fieldMetaBase[fieldName as never] as2460+ | AnyFieldLikeMetaBase2461+ | undefined) ?? defaultFieldMeta2462+ fieldMetaBase[fieldName as never] = {2463+ ...existing,2464+ isTouched: true,
okxint(GitHub profile, opens in a new tab) linked this to fix #2299
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 27c418fbe6d4e68c754de671f0460829573872b7
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 14 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR