patterns for async validation + resolvers? #9005
-
Hey guys, I have a tricky situation that I ran into and I'm not sure what the best solution for that is. Here's the basic example that I'm starting with, it's a form that uses Now what I want to do is add an async validation for the username to see if it's already taken. The problem is, if I add it to the |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 22 replies
-
could you throttle the request inside the schema validation? |
Beta Was this translation helpful? Give feedback.
-
@biowaffeln I keep running into this issue over and over and couldn't come up with a reasonable solution myself yet, except what you have already experimented with and throttling like @bluebill1049 suggested. My current approach for async validations is quite cumbersome to maintain: I do basic vailidations (type, length, ... basically anything I can capture by regex) by zod schema and then proper async validations against an API on component level, passing validation states back and forth between Have you come up with a better approach yet? |
Beta Was this translation helpful? Give feedback.
-
I use Yup for a very similar use case (detecting whether a Library name is already in use). The Yup rule looks like this: name: Yup.string()
.required("Name is required")
.test("unique-name",
"That name is already in use",
async function (this) {
return validateLibraryNameUnique(ToModel.LIBRARY(this.parent));
}
), A couple of notes about this usage:
From the docs I've read on Zod, it can do something similar, but I've never tried it. If your link to your server is slow, and you really want the validation to be asynchronous, then of course this pattern won't help much. |
Beta Was this translation helpful? Give feedback.
-
I solved this via a special https://stackblitz.com/edit/vitejs-vite-x1cial?file=src%2Fuse-refinement.ts Let me know, what you think! |
Beta Was this translation helpful? Give feedback.
-
I have a similar issue: Some of my fields need to be validated by sending a request to some validation server and I'd like to avoid sending those requests on all changes of unrelated fields. It would be nice to be able to let it behave like: Only validate on submit and on changes the specific field. |
Beta Was this translation helpful? Give feedback.
-
Does it works on Controller component with multiple MUI Textfields? |
Beta Was this translation helpful? Give feedback.
-
If you use a validation library that allows contextual validation like Yup or Joi, please upvote the feature request #11960 and its associated pull request #11961. The idea with this feature request is for the library to provide the validation event ( Note that Zod does not support contextual validation (see this issue). |
Beta Was this translation helpful? Give feedback.
I solved this via a special
useRefinement
hook. Here is an example on how to use it:https://stackblitz.com/edit/vitejs-vite-x1cial?file=src%2Fuse-refinement.ts
Let me know, what you think!