resolvers: zod's superRefine not working properly
Describe the bug
I want to use zod’s superRefine validation function. hook form shows that form is invalid, but errors object is empty
To Reproduce Steps to reproduce the behavior:
- Go to https://stackblitz.com/edit/vitejs-vite-dwvjn3?file=src/App.jsx
- See that
isValidisfalsebuterrorsobject is empty
Codesandbox link (Required) https://stackblitz.com/edit/vitejs-vite-dwvjn3?file=src/App.jsx
Expected behavior
error object should have Block is too short error message
Desktop (please complete the following information):
- OS: macOS Ventura 13
- Browser Chrome
- Version 111
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 30
- Comments: 34 (11 by maintainers)
I’m facing the same issue using
superRefine.Any update on this?
I’m running into the same issue. In my case the superRefine is needing to flag an error on a field which has not been edited. This field is only required if another field has a given value. When this happens, RHF is recognizing that the form is invalid but doesn’t add the error to the field or form.
I can get around this, mostly, by using the trigger function on the form to tell RHF to validate the fields that were never edited.
You can see this in the Codesandbox link in the original error report. When you run the sandbox as-is, the form is invalid with no errors. But if you add the following at line 24 then the error message appears on the form:
This is not quite optimal as I need to remember to call trigger to force RHF to fully recognize the validation error.
Bottom line, if superRefine is flagging an error on a field which is not dirty (not touched?), somehow add a call to trigger tied to an event on the form which tells the form to check if the field is valid. For example:
This fix worked for me. tldr:
Kudos @bluebill1049 for the debug code; I wouldn’t have found this issue without it. 💯
I had same issue and I have tried adding proper path to
addIssueand now it works. Before adding path, error was there without a path.That’s fair, I will come up with a workaround for that.
I will note though, that the
isValidproperty still shows invalid even though there are no errors since Zod reports errors but they don’t exist within the fields. I would expect that the errors object would have something if theisValidproperty is reporting invalid.This is still happening 😭
Same issue here, I have an object where you need to fill in a start and end time, i have a refine on the object that checks if the start_time is before the end_time, i can throw the error on the start_time like this.
But i want to make use of the ‘root’ property in the errors so i can show the error at the end of the form like we do with server errors
but throwing this error will make the form pass but the object its sending is empty
with
superRefinethis also worksbut this doenst
Hi! Any update on this issue? I’m manually triggering the validation if isValid = false and all the inputs are filled. But it will be nice to have a native solution.
A separate issue, hook form is field level based for validation and consistent validation strategy. I don’t have a solution in my head without re-render the entre form on each form state.