Comments (4)
Right, if you have set SUPERFORMS_LEGACY, no files will be allowed. It's mentioned here, but as always you can never mention something enough, so I'll add it in a couple of more places.
from sveltekit-superforms.
If it works on Stackblitz, it's hard to say what exactly in your code or environment is not making it work. Sometimes it's as simple as a missing name attribute on the file input field.
Here's a working example of mine anyway, maybe you can figure it out from there: https://stackblitz.com/edit/superforms-2-file-uploads?file=src%2Froutes%2F%2Bpage.svelte,src%2Froutes%2Fschema.ts,package.json
from sveltekit-superforms.
I think it's something with superValidate() that doesn't seem to work for me. If I await request.formData(), the file is there.
+page.server.ts
const schema = z.object({
image: z
.custom<File>((f) => f instanceof File, 'Please upload a file.')
.refine((f) => f.size < 1024 * 1024 * 10, 'Max 10 MB upload size.')
});
export const actions = {
default: async ({ request }) => {
const formData = await request.formData();
const form = await superValidate(formData, zod(schema)); // Doesn't matter if i put in request instead of formData
console.log((formData.get('image') as File).size); // Works
console.log(form.data.image.size); // Doesn't work (undefined)
...
}
}
+page.svelte
<script lang="ts">
import type { PageData } from './$types';
import Header from '$lib/components/Header.svelte';
export let data: PageData;
</script>
<Header />
<form method="POST" enctype="multipart/form-data" class="pt-56">
<input type="file" name="image" />
<button>Submit</button>
</form>
from sveltekit-superforms.
Solved it!
Needed to change
const form = await superValidate(formData, zod(schema));
to
const form = await superValidate(request, zod(schema), {
allowFiles: true
});
Does it have something to do with SUPERFORMS_LEGACY option in vite.config.ts? Maybe add it to the documentation.
from sveltekit-superforms.
Related Issues (20)
- Loading Timers Broken HOT 2
- Checkbox workaround does not fire onChange event HOT 2
- onChange target is generic Element insted of HTMLInputElement or an element with value property HOT 5
- Enums without default values HOT 9
- Bad type inference with nested zod schema
- Update CRUD tutorial to v2
- Binding to an optional checkbox item via a component field causes an infinite loop in SSR HOT 2
- Trouble using $posted store HOT 3
- Option for posting only tainted fields HOT 1
- Dates become undefined if schema validation produces error in form action HOT 3
- Add possibility to pass options to validation libraries (Valibot) HOT 3
- Circular dependency warning when using @sveltejs/adapter-node HOT 10
- All validation libraries from optional dependencies are installed in dev environment HOT 6
- formFieldProxy: Argument of type 'string' is not assignable to parameter of type 'never' HOT 13
- valibot: schema with nullable fields throws error on submit while equivalent schema in zod does not HOT 3
- Reset initial form data HOT 3
- How to dynamically add and remove text input fields correctly? HOT 1
- Dynamic Default Values? HOT 16
- Installing Superforms Causes Cloudflare Deployment to Fail HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sveltekit-superforms.