Repository navigation
[field] Fix Form error matching in Field.Error - #4494
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Codex Review (GPT-5.4)
This PR is a narrow bug fix to Field.Error matching when Form.errors is present. I reviewed the full branch diff against a freshly fetched upstream/master, and the final change now looks internally consistent with the intended fallback behavior.
1. Bugs / Issues (None)
2. Root Cause & Patch Assessment
The previous logic let any Form.errors[name] value bypass match filtering, which forced every matched Field.Error visible. The patch fixes that by keeping string match values tied to validity-specific rendering while preserving match={false} and an omitted match as the default/server slot. That addresses the reported bug without forcing a larger public API change.
3. Pattern Consistency Assessment
The final implementation is easier to follow after the cleanup that reuses hasSpecificMatch in both render gating and message selection. The behavior split between match={true}, string matches, and default-slot matches is now explicit instead of being an incidental side effect of formError.
4. Test Coverage Assessment
| Validation | Result | Notes |
|---|---|---|
pnpm test:jsdom FieldError --no-watch |
Pass | FieldError.test.tsx passed with the added regression coverage |
pnpm eslint packages/react/src/field/error/FieldError.tsx packages/react/src/field/error/FieldError.test.tsx |
Pass | No targeted lint issues |
The added tests cover both of the risky paths introduced by the fix: server-error fallback through match={false} and client-validation fallback through the same default slot.
Merge Confidence Score
Overall merge confidence is 4/5. The diff is small, the behavior change is now well targeted, and the focused validation covered the touched runtime path and its regression tests. I would merge this as-is.
Claude Opus 4.6 Review: [field] Fix Form error matching in Field.Error
Summary
The bug: when Form.errors contains an error for a field, every Field.Error for that field renders — even those with a specific string match like "valueMissing" or "tooShort". This happens because the old logic short-circuits on formError in the very first branch:
// OLD (buggy)
if (formError || match === true) {
rendered = true; // all Field.Error instances render when formError existsThe fix correctly scopes the formError to only trigger the default slot (match={false} or match omitted), keeping string-based match values tied exclusively to client-side ValidityState.
Logic walkthrough
The new rendering logic:
match value |
Behavior |
|---|---|
true |
Always rendered (unchanged) |
string (e.g. "valueMissing") |
Only when validityData.state[match] is true — form errors never force this |
false / undefined (default slot) |
Boolean(formError) || validityData.state.valid === false — form errors do trigger this |
The message selection:
const errorMessage = hasSpecificMatch ? clientErrorMessage : formError || clientErrorMessage;- String match → always uses client validity message
- Default slot → prefers server/form error, falls back to client error
This is correct. The hasSpecificMatch variable is reused cleanly across both the render-gating and message-selection paths.
Test coverage
The two new tests cover both critical paths:
- Form error with string-matched slots present — verifies string-matched errors stay hidden while
match={false}renders the form error - Client validation with
match={false}— verifies the default slot still works for native constraint validation when no form error exists
Both tests are well-structured and follow the existing conventions.
Potential concerns
None blocking. One minor observation:
- When
match={true},hasSpecificMatchisfalse, soerrorMessage = formError || clientErrorMessage. This meansmatch={true}prefers the form error message over the client error. This seems intentional and reasonable for the "always show" escape hatch, but it's worth noting thatmatch={true}is now sensitive to form errors in message content (though not in rendering, since it always renders anyway). This is arguably better than the old behavior.
Verdict
Clean, minimal fix. The logic change is sound, the edge cases are handled correctly, and the test coverage is adequate. Approve.
Relates to #1923
This tightens
Field.Errorso string-basedmatchvalues stay scoped to client validity even when aFormerror is present. It also preserves the currentmatch={false}fallback behavior so server and default errors can still render through the default slot.Changes
Form.errorsfrom force-rendering every matchedField.Error.match={false}and an omittedmatchaligned as the default error slot.