Skip to content

fix: clear server-side field errors on edit (SDK-923)#2202

Open
jeffredodd wants to merge 4 commits into
mainfrom
fix/SDK-923-clear-server-errors-on-edit
Open

fix: clear server-side field errors on edit (SDK-923)#2202
jeffredodd wants to merge 4 commits into
mainfrom
fix/SDK-923-clear-server-errors-on-edit

Conversation

@jeffredodd

@jeffredodd jeffredodd commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes SDK-923 — server-side validation errors on form fields blocked resubmission until a page refresh (reported on Pay Schedule date fields, but the bug is SDK-wide).

  • useSyncFieldErrors (SDKFormProvider) maps server fieldErrors onto RHF via setError(name, { type: 'custom', message }).
  • useField.handleChange never called clearErrors, and forms run mode: 'onSubmit', so RHF didn't reclear on change either. The custom error sat there forever.
  • Fix: in handleChange, when the field already has a type: 'custom' error, call clearErrors(name) before forwarding the new value. Client-side validation errors (required, min, zod-driven, etc.) are untouched — RHF's normal validation loop is unaffected.

Despite the ticket framing, this was not a state-machine problem — PaySchedule already uses a robot3 machine. The bug was in the shared form layer, so the fix lives there and benefits every form, not just Pay Schedule.

Test plan

  • npm run test -- --run src/components/Common/Fields/hooks/useField.test.tsx (22 pass; 3 new)
  • npm run test -- --run src/partner-hook-utils/form/ src/components/Company/PaySchedule/ (152 pass)
  • npm run test -- --run src/components/Common/Fields/ (51 pass)
  • Verified the new positive tests fail without the fix and pass with it (red → green); the negative test (client-side required error not cleared on edit) passes in both states, confirming the change is surgical.
  • Manual repro in sdk-app: open Pay Schedule, submit a date combination that trips a server validator, confirm the date error appears, edit the date, confirm the error clears, then resubmit successfully.
  • Spot-check a non-date field elsewhere (e.g., Compensation rate) to confirm the same clear-on-edit behavior across field types.

🤖 Generated with Claude Code


const handleChange = (updatedValue: TValue) => {
const value = transform ? transform(updatedValue) : updatedValue
if (fieldState.error?.type === 'custom') {

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.

Confirmed errors are coming back as type custom from the server. Seems save if a user is indeed changing the input.

@jeffredodd jeffredodd marked this pull request as ready for review June 18, 2026 15:27
@jeffredodd jeffredodd requested a review from a team as a code owner June 18, 2026 15:27
@jeffredodd jeffredodd marked this pull request as draft June 24, 2026 16:29
@jeffredodd jeffredodd force-pushed the fix/SDK-923-clear-server-errors-on-edit branch 2 times, most recently from 93ed3b5 to a39ef68 Compare June 24, 2026 17:55
@jeffredodd jeffredodd marked this pull request as ready for review June 25, 2026 17:57
`SDKFormProvider` mirrors server `fieldErrors` onto the form via
`setError(name, { type: 'custom', ... })`. Without a clear-on-edit
behavior in this form, that error stays set until a full refresh,
blocking resubmission after the user corrects a date — the original
SDK-923 repro.

Subscribe to `formMethods.watch` inside `usePayScheduleForm` and clear
any `type: 'custom'` error on a field as soon as the user edits it.
RHF's values subject only fires for actual value changes (not blur),
so the subscription is narrow.

Client-side validation errors (`required`, `min`, zod-driven, etc.)
are untouched — the type filter only matches the server-error path.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jeffredodd jeffredodd force-pushed the fix/SDK-923-clear-server-errors-on-edit branch from a39ef68 to eee8731 Compare June 25, 2026 17:57
Comment on lines +312 to +327
// Server-side validation errors (mirrored via SDKFormProvider as
// `type: 'custom'`) need to clear the moment the user edits a field;
// otherwise the stale error blocks resubmission until a refresh (SDK-923).
useEffect(() => {
const subscription = formMethods.watch((_, info) => {
const { name } = info
if (!name) return
if (formMethods.getFieldState(name).error?.type === 'custom') {
formMethods.clearErrors(name)
}
})
return () => {
subscription.unsubscribe()
}
}, [formMethods])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i'm a little worried about doing this subscription based. is this watching each field on the form?

I'm wondering if we could do an event based fix here instead where we could detect changes to the problem fields and clear them if an error is present?

if we keep this approach i think we need to include each formMethod function as an entry in the dep array individually. formMethods in the dep array used to cause infinite renders (maybe they fixed it?)

jeffredodd and others added 3 commits June 26, 2026 10:59
…vider

Address Steve's review: replace the per-render `formMethods.watch` subscription
in `usePayScheduleForm` with an event-based clear inside `useField.handleChange`,
so the fix lives in the shared form layer and only fires for the field actually
being edited. Pair it with an `appliedRef` in `useSyncFieldErrors` so the effect
no longer re-applies server errors on every render (which was overwriting the
clear and trapping users in a stale error until refresh). Relocate the
SDK-923 unit tests onto `useField` and add an 8-case permutation suite for
`SDKFormProvider` error clearing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…vider

useField is the generic SDK input hook and shouldn't carry form-error policy.
Move the type:'custom' clear into useSyncFieldErrors using RHF's per-field
`subscribe` API (RHF 7.55+), so we listen only to the fields that actually
carry a server error rather than watching the whole form. Switch
applied-tracking from a content-string Set to a WeakSet keyed by SDKFieldError
identity so an identical-message re-submission still re-applies the error
(new SDKError from composeSubmitHandler → new field-error refs). Drop the
matching useField unit tests; the SDKFormProvider permutation suite covers it.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Revert the SDKFormProvider changes and the generic useField changes — the
fix belongs in the PaySchedule form, not in shared infrastructure. Use RHF's
per-field `subscribe` (RHF 7.55+) inside `usePayScheduleForm` against ONLY
`anchorPayDate` and `anchorEndOfPayPeriod` so we listen exclusively to the
fields that can carry a server validation error. On edit of one of those
fields with a `type: 'custom'` error: clear the field error AND call
`setSubmitError(null)` to drop the upstream source, which prevents
`SDKFormProvider`'s sync effect from re-applying the error on the next
render. Restore the PaySchedule-local SDK-923 tests (plus negative cases:
client-side errors and non-date fields are unaffected).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants