TheEdoRan / next-safe-action

Type safe and validated Server Actions in your Next.js (App Router) project.
https://next.next-safe-action.dev
BSD 3-Clause "New" or "Revised" License
1.38k stars 26 forks source link

[BUG] Issue When Using useAction to Create Form with File Schema #126

Closed KhoeLe closed 2 weeks ago

KhoeLe commented 2 weeks ago

Are you using the latest version of this library?

Is there an existing issue for this?

Describe the bug

I am experiencing issues when creating a form that includes a file upload using useAction. Below is the code where the problem occurs:

Reproduction steps

form-action.tsx image

logic code handle submit image

form input upload file image

Expected behavior

Problem occurs during the form submission process. image

Reproduction example

https://github.com/KhoeLe/next-safe-action.git

Operating System

Windows 10

Library version

"next-safe-action": "^6.2.0",

Additional context

if I remove the file from the formDataSchema and any related components, the form submits successfully image

No response

TheEdoRan commented 2 weeks ago

Not related to next-safe-action, got the same error with your schema and without using this library. You need to pass the file in FormData, I suggest using zod-form-data on top of Zod to validate the form.

KhoeLe commented 2 weeks ago

Hello @TheEdoRan

I am having issues using react-hook-form with a Zod schema for file uploads (z.instanceof(File)). I am encountering errors. Do you have any ideas for resolving this?

andourys commented 2 weeks ago

Hello @TheEdoRan

I am having issues using react-hook-form with a Zod schema for file uploads (z.instanceof(File)). I am encountering errors. Do you have any ideas for resolving this?

File isn't a type noticed by nextjs if your node version is below >= 20.0. The potential alternative may be to try z.instanceof(Blob)), I haven't tried it out myself though

ericd33 commented 1 day ago

I'm having the same problem, is there a workaround?