-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issues with Handling File Inputs and Image Previews in Next.js 15 Form Actions #72958
Comments
model of server actions is broken, its reset form everytime even when form action return errors (in docs Next recommend to return errors in objects - https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#server-side-form-validation). As for me, i decide to use formData cycle for error state. In server action Next passes formData, we can return object with formData from server action and can pass values in inputs after. Next resets values, we returns them back :) P.S. You can use this in all cases (not only for error state) in your example for some kind of inputs |
Thank you for your response! Your idea is excellent—I can’t believe I didn’t think of it myself. Unfortunately, it doesn’t resolve the issue I’m facing. While I do have access to the formData object after validation, I’m unable to programmatically push file data into the input due to security restrictions. This limitation is particularly challenging for file inputs. For other input types, I’ve worked around it by creating a custom input component that stores the input data in state and programmatically updates it. |
@Marabii, try to add state.formData.get('images') in defaultValue prop and as initValue in useState |
Thanks for trying to help me but it doesn't work. I really think there is no way to do this unless they developed a specific Input file component for ths usecase. |
@Marabii in order to preserve file you can use useState hook and then manually append it to formData on form submit:
|
@Marabii yeah, I was also disappointed, it seems like useState-less form handling is what they are after here with serverActions, but it supports only basic use cases with plain inputs for now. Even select does not work without additional hassle. |
I reopened this issue per @liri2006 request, it seems like manual manipulation of the formdata object is mandatory in cases like file inputs and select inputs. |
Link to the code that reproduces this issue
https://github.com/Marabii/next-js-issue
To Reproduce
Minimal Reproducible Example:
Client Component (
Home.tsx
):Server Action (
action.ts
):Steps to Reproduce:
selectedImages
state.Current vs. Expected behavior
Description:
I'm experiencing challenges when working with file inputs, specifically image uploads, in a Next.js 15 application using form actions. The issues arise when attempting to preview selected images and implement a remove functionality. Below are the detailed problems and a minimal reproducible example.
Issues Encountered:
Image Inputs Reset on Submit with Validation Errors:
selectedImages
state on the client side still holds the previously selected images and displays the previews.Inability to Synchronize File Input with Selected Images State:
value={selectedImages}
) due to security restrictions.selectedImages
state, especially when users remove images from the preview. The file input remains unchanged, leading to inconsistencies.Impact:
These issues hinder the user experience by creating confusion between the displayed image previews and the actual state of the file input. Users may find it frustrating to re-upload images after encountering validation errors, and the inability to manage the file input state programmatically limits the functionality of the form.
Expected Behavior:
On Validation Errors:
Synchronizing File Input with State:
selectedImages
state. I know it's not currently possible due to security reasons, but there has to be a workaround.Provide environment information
Which area(s) are affected? (Select all that apply)
Not sure
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local), next start (local), Vercel (Deployed), Other (Deployed)
Additional context
No response
The text was updated successfully, but these errors were encountered: