Skip to content

[field] Fix Form error matching in Field.Error - #4494

Merged
atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/field-error-server-slot
Apr 3, 2026
Merged

atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/field-error-server-slot

Conversation

@atomiks

@atomiks atomiks commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor

Relates to #1923

This tightens Field.Error so string-based match values stay scoped to client validity even when a Form error is present. It also preserves the current match={false} fallback behavior so server and default errors can still render through the default slot.

Changes

  • Prevented Form.errors from force-rendering every matched Field.Error.
  • Kept match={false} and an omitted match aligned as the default error slot.
  • Added regression coverage for server-error and client-error fallback behavior.

@atomiks atomiks added type: bug It doesn't behave as expected. component: field Changes related to the field component. labels Mar 31, 2026 — with ChatGPT Codex Connector
@pkg-pr-new

pkg-pr-new Bot commented Mar 31, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4494
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4494
    

commit: 7a5255d

@mui-bot

mui-bot commented Mar 31, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+29B(+0.01%) 🔺+25B(+0.02%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Mar 31, 2026

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 0827b76
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69cbabd73cfc4e00082e7633
😎 Deploy Preview https://deploy-preview-4494--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Mar 31, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 7a5255d
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69cf6e38cfe50000086037fd
😎 Deploy Preview https://deploy-preview-4494--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks atomiks left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 exists

The 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:

  1. Form error with string-matched slots present — verifies string-matched errors stay hidden while match={false} renders the form error
  2. 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}, hasSpecificMatch is false, so errorMessage = formError || clientErrorMessage. This means match={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 that match={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.

@atomiks
atomiks marked this pull request as ready for review March 31, 2026 11:24
@atomiks
atomiks merged commit a059dd2 into mui:master Apr 3, 2026
7 checks passed
@atomiks
atomiks deleted the codex/field-error-server-slot branch April 3, 2026 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: field Changes related to the field component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants