Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MattisAbrahamsson opened this issue Jan 30, 2024 · 6 comments
Open

Comments

@MattisAbrahamsson
Copy link

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
Copy link

Kkinod commented Mar 20, 2024

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
Copy link

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!

@shessafridi
Copy link

Is there any workaround for this?

@kamyarkaviani
Copy link

Is there any workaround for this?

I guess you can clear the errors yourself by calling form.clearErrors(fieldName); in the onChange of the element.

@jeremy-habit
Copy link

Up ! is there a clean way to achieve this ?

@jeremy-habit
Copy link

Up ! is there a clean way to achieve this ?

found a nice workaround from RHF: colinhacks/zod#2220 (comment)

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

No branches or pull requests

6 participants