Skip to content

Want to use result data from zod as FormData sent to remix action fn #120

Answered by airjp73
danecando asked this question in Q&A
Discussion options

You must be logged in to vote

If you're using zod, this transformation can be added to the zod schema. Then you'll get the transformed value both in onSubmit and when you validate in your action.

const validator = withZod(z.object({
  myDate: z
    .string()
    // I don't think these validations are built-in so these would be your functions
    .refine(isDate)
    .transform(toIsoDate),
})

Then in your action

const result = await validator.validate(await request.formData());
if (result.error) return validationError(result.error);

// `myDate` is an ISO date string
const { myDate } = result.data;

Or in your onSubmit

onSubmit=({ myDate }) => {
  // `myDate` is an ISO date string
})

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@danecando
Comment options

Answer selected by danecando
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants