Repository navigation
🐞 fix(useForm): mark fields named after Object.prototype members as touched on blur and setValue - #13833
Conversation
| if ( | ||
| !isPreviousFieldTouched || | ||
| typeof isPreviousFieldTouched === 'function' | ||
| ) { |
There was a problem hiding this comment.
I’d prefer not to special-case function values here. Since the issue is inherited Object.prototype members, can we fix the underlying property lookup instead?
Signed-off-by: Charan Rathore <[email protected]>
6cdfac5 to
9dae5b5
Compare
|
Good point, reworked. The blur path now does the property lookup through the library's own own-aware path helper instead: const isPreviousFieldTouched = has(_formState.touchedFields, name)
? get(_formState.touchedFields, name)
: undefined;
Verified: the new proto-member tests fail without this change and pass with it, full suite green (128 suites, 1514 tests), typecheck/lint/prettier clean. |
Pull Request Template
Proposed Changes
Follow-up to #13832, which fixed dirty-value extraction for a field literally named
hasOwnPropertyand disclaimed this touched-fields path.Two places in
createFormControlbreak when a field is named after anObject.prototypefunction member (hasOwnProperty,toString,valueOf, ...):delayErrorCallbacks[name]resolves the inherited prototype function for such a name, and invoking it throws inside the blur handler.updateTouchAndDirty'sisPreviousFieldTouchedguard reads the same inherited function as truthy, so the field is never marked touched, on both the blur path andsetValue(..., { shouldTouch: true }).Fix: an own-key guard (
Object.prototype.hasOwnProperty.call) at thedelayErrorCallbackssite, and atypeof === 'function'guard at the touched check, so an inherited function never counts as previously touched.Tests added: a blur regression test for a
hasOwnPropertyfield, and asetValueshouldTouchtest coveringhasOwnPropertyandtoString.Deliberately out of scope: read-side quirks such as reading
formState.touchedFields.hasOwnPropertydirectly returning the inherited function before the field is touched. Fixing that means guarding the hot-pathget()helper and is a perf/bundle-size trade-off for its own discussion.Verification (run locally on this branch): without the production change the two focused suites fail 3 tests; with it all 115 focused tests pass. Full Jest run: 128 suites, 1514 tests, 26 snapshots green. Prettier clean on the changed files. Not run: typecheck, type tests, build, ESLint, browser E2E, API extractor.
Related: #13832
Fixes # (no issue, follow-up to #13832)
Type of change
Checklist:
This change was prepared with AI assistance and verified locally as described above.