react-hook-form / resolvers

📋 Validation resolvers: Yup, Zod, Superstruct, Joi, Vest, Class Validator, io-ts, Nope, computed-types, typanion, Ajv, TypeBox, ArkType, Valibot, effect-ts and VineJS
https://react-hook-form.com/
MIT License
1.67k stars 151 forks source link

Zod superRefine does not correctly revalidate the form on change #661

Open MattisAbrahamsson opened 5 months ago

MattisAbrahamsson commented 5 months ago

Describe the bug The zod resolver doesn't seem to run the superRefine function correctly on form changes, it only works when changing the field defined in the path of the issue, but in this case I have errors based on the existance of data in other fields, so I expect this to work

To Reproduce Steps to reproduce the behavior:

  1. Go to the Codesandbox below
  2. Write something in the "Last name" field
  3. The "First name" field should get an error because the refine function requires first name to be defined if last name is
  4. Writing something in the "First name" field will correctly remove the error, but removing the content in the "Last name" field will not

Codesandbox link (Required) https://codesandbox.io/p/github/MattisAbrahamsson/rhf-zod-refine-minimal-reproduction/main

Expected behavior I expect the resolver to revalidate my form when it changes, since reValidateMode is set to onChange by default, and I have not opted out of it.

Kkinod commented 3 months ago

We have exactly the same error, we have it set that the start date must be smaller than the end date and when we set the start date later than the end date, an error appears, of course, but now if we set the end date to a later date than the start date, even though validation passes (correct behavior), the error does not disappear

briavicenti commented 3 months ago

Same error for us! We have a refine on a zod schema that applies an error message to the second of two form fields if the combination of both of the form fields is incorrect. We are only applying the error to the second field as that's where we would like to show the error message and autofocus on failed validation. The form does correctly pass validation if the first form field is updated, but the error on the second field never disappears. Would love to see this fixed!