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]: Resolver does not validate to reflect correct error state without manual form.trigger() #671
Comments
We're having a similar issue, described in a comment here! I think that these two issues are dupes. |
I have a similar issue. The refine function is triggered and resolved but error from formState.erros does not disappear -> if I call form.trigger() in every onChange field callback then everything works good and errors are cleared form formState. |
@MusaGillani the interesting thing is that when you add rules={{ deps: ['field1', 'field2'] }} to FormField (which passthrough to Controller) it works as expected :D |
Interesting, |
I found it by accident. I was looking for something like "dependencies" because I worked with Ant Design for a very long time and there I used it a lot to solve this kind of problem. Once I found this in the rules object, I set it up, tested it and it actually works, but why isn't there anything about it in the documentation? Maybe it also works by accident :D |
yeah the docs seem pretty arcane atp, i wonder if it uses trigger() under the hood, since the validations were working fine with updates to formstates as well but re-renders were not being "triggered" |
Maybe, but I do not think so. The problem with using trigger() is that in the case of mode="onSubmit" (default) the error will be displayed even before the form is submitted but using rules.deps, the error appears only after submit. |
yeah we're using here's the snippet for that useEffect(() => {
if (form.formState.submitCount === 0) {
return;
}
const subscription = form.watch(() => form.trigger());
return () => subscription.unsubscribe();
}, [form.watch, form.trigger, form.formState, form]); |
Describe the bug
Form Validation doesn't re render the correct error message to reflect correct
error
fromformState
To Reproduce
Steps to reproduce the behavior:
Base
labelled input fieldBase
input field remove the errors from the UIScreen recording showing validation not running:
Video
Sandbox link
Expected behavior
Validation should trigger when changing a any other field than base ,
due to formState update causing resolver to re-validate the form data
Desktop:
Additional context
The error is fixed using
form.trigger()
directly in theonChange
of the<Input/>
component, which removes the error from the UI when changing the input value.Maybe the formState is being updated correctly but the resolver does cause a re-render?
Screen Recording, showing form.trigger() added to the
Overtime
label triggers the validationVideo
The text was updated successfully, but these errors were encountered: