TanStackT
TanStack17mo ago
3 replies
popular-magenta

useQuery in Field validators

Is there a way to use
useQuery
in Field validators?

I have a Form where the user inputs two dates and I make a Request to the Backend to check if those Dates are on a public holiday or not (which would mean the user needs to select a different Date).

To use the Caching from useQuery I would like to define my Query somewhere

function useIsPublicHoliday(date: Date) {
  return useQuery({
   // …
  })
}


and then use it in the validation on the Field

<Field 
  name={…}
  validators={{
    onBlur: () => {
      // ?? How do get the the public holiday data here ??
    }
  }}
  children={…}
/>


I checked the docs and this example but it only uses Promises in the validators:
https://tanstack.com/form/latest/docs/framework/react/examples/query-integration
Was this page helpful?