Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A Zod refinement error does not appear or does not clear in React Hook Form

ErrorThe confirm password error stays after the password is fixed

Short 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.

  • Applies to react-hook-form 7, zod 4, @hookform/resolvers, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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));
  2. 2Match what you see, then jump to its fix

    1. 1The message is not under the field name in errorsThe error has no path, so it is not attached to the fieldFix
    2. 2The cross-field message appears only after every other field is validZod does not run the refinement while another issue blocks itFix
    3. 3The error on confirm does not clear when only password is editedThe resolver re-validates one field at a timeFix
    4. 4Nothing shows until the first submitThe validation mode only runs on submitFix

What does the error look like?

  • The confirm password error stays after the password is fixed
  • The refinement error never shows until every other field is valid
  • The 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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.