Repository navigation
fix(setValues): update fields registered under an object or array value - #13667
Merged
bluebill1049 merged 1 commit intoAug 20, 2026
Merged
bluebill1049 merged 1 commit into
bluebill1049 merged 1 commit into
Conversation
`setValues` flattened the incoming values and matched mounted field names against the resulting leaf paths. A field registered at a path that holds an object or an array therefore never matched, because `flatten` only emits leaf paths, so its input was left untouched. This affected multiple selects and checkbox groups, whose value is an array under a single field name, and any container path that a `Controller` registers directly. An empty array or object was dropped altogether, since it flattens to no keys at all. Match mounted field names against the values by walking the path instead, so a container value reaches `_setValue`, which already knows how to apply it. Path presence is checked with own properties so that an explicitly provided `undefined` still propagates while an absent field stays untouched.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed Changes
setValuesdoes not update a field that is registered at a path holding an object or an array. The input keeps its previous value even thoughgetValues()reports the new one.The batch loop flattens the incoming values and then matches mounted field names against the resulting keys:
flattenonly emits leaf paths, so{ tags: ['b', 'c'] }becomes{ 'tags.0': 'b', 'tags.1': 'c' }. A field registered astagsis never in that map, so_setValueis skipped and the element is left alone. An empty array or object is worse: it flattens to no keys at all, so the update disappears entirely.In practice this hits the DOM controls whose value is an array under a single field name:
<select multiple {...register('tags')} />keeps its old selectionControllernever receives the new valuesetValues({ tags: [] })does not clear anythingsetValue('tags', ['b', 'c'])handles all of these correctly today, so the two APIs disagree.This replaces the flatten-and-lookup with a path walk over the provided values, so a container value is passed to
_setValueas-is._setValuealready knows how to apply object and array values. It routes them throughsetFieldValue/setFieldValues, which handle multiple selects, checkbox groups and nested objects.Path presence is resolved with own properties rather than
in, which keeps two existing behaviours intact: an explicitly providedundefinedstill propagates to a mounted field, and a field name that collides with an inherited property (toString) is not treated as provided. The newhasutil mirrorsget's path semantics, including a path stored as a single literal key such as{ 'a.b': 1 }, sohasandgetagree on what is present.flattenitself is unchanged and still used byjsonToFormData, where leaf-only output is what is wanted.No linked issue. Found while reading the
setValuesbatch path.Type of change
Checklist:
Tests
Three regression tests in
setValues.test.tsxcover a multiple select, a checkbox group, and clearing a multiple select with[]. All three fail onmaster:hasalso gets its own unit test alongside the othersrc/utilstests.Full suite after the change:
pnpm lintreports no new warnings,pnpm typeandpnpm test:typeare clean, andpnpm buildsucceeds. Bundle impact ondist/index.cjs.jsis +45 B gzipped (13739 B to 13784 B), within the 14.0 kB bundlewatch budget.