如何使用zod在混音中处理多个表单?:假设我想获得一些名为“_intent”的输入值,并根据该值解析正确的表单数据。我面临的问题是,我会收到错误,因为不是所有表单都发送相同的formData...
wlzqhblo1#
最后我做了这个:
export const action = async ({ request }: ActionArgs) => { const formData = await request.formData(); const allFormDataFields = Object.fromEntries(formData.entries()); // first extract the intent const intent = allFormDataFields._intent as keyof typeof FormIntents; if (intent === FormIntents.add_transaction) { // now parse with relevant zod object const result = myZodObject.safeParse(allFormDataFields); if (!result.success) { return badRequest({ errorFields: result.error.flatten(), }); } // do stuff } if(intent === "something else"){ // do other stuff }
1条答案
按热度按时间wlzqhblo1#
最后我做了这个: