Giter Club home page Giter Club logo

Comments (4)

ciscoheat avatar ciscoheat commented on May 27, 2024 1

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.

ciscoheat avatar ciscoheat commented on May 27, 2024

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.

EmilZackrisson avatar EmilZackrisson commented on May 27, 2024

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.

EmilZackrisson avatar EmilZackrisson commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.