A Zod refinement error does not appear or does not clear in React Hook Form
The confirm password error stays after the password is fixedShort answer
Three Zod and React Hook Form rules explain it. The resolver re-validates only the field being edited, Zod skips refinements while a type error exists, and an error without a path lands on the form root. Trigger the dependent field, add a when condition, set path, and choose a mode that validates on change.
Fix it fast
1Find out which cause you have
Log the whole errors object after a failed submit. If your message sits somewhere other than the field you expected, the cause is the path. If it is missing, check the refinement order.
console.log(JSON.stringify(formState.errors, null, 2));2Match what you see, then jump to its fix
- 1
The message is not under the field name in errorsThe error has no path, so it is not attached to the fieldFix - 2
The cross-field message appears only after every other field is validZod does not run the refinement while another issue blocks itFix - 3
The error on confirm does not clear when only password is editedThe resolver re-validates one field at a timeFix - 4
Nothing shows until the first submitThe validation mode only runs on submitFix
- 1
What does the error look like?
The confirm password error stays after the password is fixedThe refinement error never shows until every other field is validThe refinement message is not shown next to the field it belongs to
Cause 1 of 4
The error has no path, so it is not attached to the field
1Check
The path option sets where a refinement reports its error. Without it, the issue is not tied to the field you want to show it on.
grep -n "refine\|superRefine" -r src/2Fix
Add the path option naming the field that should show the error.
const schema = z
.object({ password: z.string().min(8), confirm: z.string() })
.refine((d) => d.password === d.confirm, {
message: "Passwords do not match",
path: ["confirm"],
});3Verify
Submit mismatched values and read errors.confirm.
console.log(formState.errors.confirm?.message);Confirmed by zod.dev
Cause 2 of 4
Zod does not run the refinement while another issue blocks it
1Check
By default refinements do not run if a non-continuable issue was already found. An invalid type on another field in the same object prevents the password check from running.
grep -n "abort\|when" -r src/2Fix
Use the when option to run the refinement as soon as the fields it compares are valid.
// baseSchema is the plain z.object({ password, confirm }) without the refine
.refine((d) => d.password === d.confirm, {
message: "Passwords do not match",
path: ["confirm"],
when(payload) {
return baseSchema
.pick({ password: true, confirm: true })
.safeParse(payload.value).success;
},
})3Verify
Fill only the two password fields and check the error.
console.log(formState.errors);Confirmed by zod.dev
Cause 3 of 4
The resolver re-validates one field at a time
1Check
React Hook Form says re-validation happens one field at a time during user interaction. Editing password does not refresh the message stored on confirm.
grep -n "zodResolver\|trigger(" -r src/2Fix
After the first field changes, trigger validation of the field that depends on it.
<input
{...register("password", {
onChange: () => trigger("confirm"),
})}
/>3Verify
Fix the password and confirm the error on confirm disappears.
console.log(formState.errors.confirm);Confirmed by react-hook-form.com
Cause 4 of 4
The validation mode only runs on submit
1Check
The default mode is onSubmit; after a submit, errored inputs revalidate on change by default.
grep -n "useForm(" -A5 -r src/2Fix
Choose a mode that validates earlier.
useForm({
resolver: zodResolver(schema),
mode: "onChange",
});3Verify
Type an invalid value and check the message appears before submit.
console.log(formState.errors);Confirmed by react-hook-form.com
How do I stop it coming back?
- Always give object-level refinements a path.
- Trigger dependent fields when one of a pair changes.
- Pick mode deliberately and test cross-field rules by editing the fields in both orders.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
The validation message never appears under an MUI X Date Picker
The field never turns red after a failed validation
A DatePicker does not take helperText or error as direct props.
3 causes, each with a fixWeb apps
React Hook Form: the submit button does nothing and onSubmit never runs
The page reloads when the form is submitted
handleSubmit runs your onSubmit only when validation passes.
4 causes, each with a fixWeb apps
React Hook Form isDirty does not go back to false
formState.isDirty is false after setValue
React Hook Form sets isDirty by comparing the current values with defaultValues.
4 causes, each with a fixWeb apps
react-hook-form: an optional number input gives NaN instead of a number or null
Validation fails on an optional numeric field
valueAsNumber returns NaN when a value cannot be converted, and setValueAs is ignored while valueAsNumber is on.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fix