IdoPesok / zsa

Home Page:https://zsa.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[issue] Having issues with FormData

fitimbytyqi opened this issue · comments

I am loving the library so far 🔥

One issue im having is when passing formdata from client side execute hook.

Here's how the hook looks like in the client:

 /** @actions */
  const { execute, isPending } =
    useServerAction(createAction, {
    });

My Submit Handler

const handleSubmit = (values) => {
    const formData = new FormData();

    formData.append('Name', "Hello");

    execute(formData);
  };
export const createAction = createServerAction()
  .input(typedAnySchema(), { type: 'formData' })
  .handler(async ({ input }) => {
    console.log(input, input instanceof FormData);
  });

I don't know why the instance of input comes false here, it should be true ?

Note: Interestingly enough, if I use the execute function like so and remove the type: 'formData' it works perfectly:

    const formData = new FormData();
    formData.append('Name', name);
    
    execute({ values: formData });
    ```

With type set to formData, ZSA coverts the incoming FormData to a JSON object so you don't need to do formData.get(...) and it also parses those values. Therefore the input in the handler is JSON that matches your Zod schema and not an instance of FormData.

And how would I go about receiving FormData instead?

Because my third party api accepts a formdata in some cases.