forge42dev / remix-hook-form

Open source wrapper for react-hook-form aimed at Remix.run
MIT License
354 stars 29 forks source link

How to use getValidatedFormData with multiple actions? #40

Closed adaboese closed 1 year ago

adaboese commented 1 year ago

I have a route that can submit either "generate" or "accept" action and their fields are different.

AlemTuzlak commented 1 year ago

@adaboese this is not supported with getValidatedFormData but is supported wit this part of the readme: https://github.com/Code-Forge-Net/remix-hook-form#validateformdata you can basically get the formData and then based on your field validate with whatever you want, eg:

const resolver1 = schema1
const resolver 2 = schema2

export const action = async ({ request }: ActionArgs) => {
  // Lets assume you get the data in a different way here but still want to validate it
  const formData = await request.formData()
  // Takes the request from the frontend, parses and validates it and returns the data
  const { errors, data } =
    await validateFormData<FormData>(formData, formData.get("is-generate") ? resolver1 : resolver2);
  if (errors) {
    return json(errors);
  }
  // Do something with the data
};