-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { superValidate, message, withFiles } from '$lib/index.js'; | ||
import { zod } from '$lib/adapters/zod.js'; | ||
import { fail } from '@sveltejs/kit'; | ||
import { schema } from './schema.js'; | ||
|
||
export const load = async () => { | ||
const form = await superValidate(zod(schema)); | ||
return { form }; | ||
}; | ||
|
||
export const actions = { | ||
default: async ({ request }) => { | ||
const formData = await request.formData(); | ||
console.log(formData); | ||
|
||
const form = await superValidate(formData, zod(schema), { allowFiles: true }); | ||
console.log(form); | ||
|
||
if (!form.valid) return fail(400, withFiles({ form })); | ||
|
||
return message(form, 'Form posted successfully!'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<script lang="ts"> | ||
import { page } from '$app/stores'; | ||
import { superForm } from '$lib/index.js'; | ||
import SuperDebug from '$lib/index.js'; | ||
import FileInput from './FileInput.svelte'; | ||
import type { PageData } from './$types.js'; | ||
import type { SubmitFunction } from '@sveltejs/kit'; | ||
export let data: PageData; | ||
let progress = 0; | ||
function fileUploadWithProgressbar(input: Parameters<SubmitFunction>[0]) { | ||
return new Promise<XMLHttpRequest>((res) => { | ||
let xhr = new XMLHttpRequest(); | ||
// listen for upload progress | ||
xhr.upload.onprogress = function (event) { | ||
progress = Math.round((100 * event.loaded) / event.total); | ||
console.log(`File is ${progress}% uploaded.`); | ||
}; | ||
// handle error | ||
xhr.upload.onerror = function () { | ||
console.log(`Error during the upload: ${xhr.status}.`); | ||
}; | ||
// upload completed successfully | ||
xhr.onload = function () { | ||
if (xhr.readyState === xhr.DONE) { | ||
console.log('Upload completed successfully.'); | ||
progress = 0; | ||
res(xhr); | ||
} | ||
}; | ||
xhr.open('POST', input.action, true); | ||
xhr.send(input.formData); | ||
return xhr; | ||
}); | ||
} | ||
const { form, errors, message, enhance } = superForm(data.form, { | ||
taintedMessage: null, | ||
onSubmit({ customRequest }) { | ||
customRequest(fileUploadWithProgressbar); | ||
} | ||
}); | ||
const acceptedExtensions = '.flac, .mp3'; | ||
</script> | ||
|
||
<SuperDebug data={$form} /> | ||
|
||
<h3>Superforms testing ground - Zod</h3> | ||
|
||
{#if $message} | ||
<!-- eslint-disable-next-line svelte/valid-compile --> | ||
<div class="status" class:error={$page.status >= 400} class:success={$page.status == 200}> | ||
{$message} | ||
</div> | ||
{/if} | ||
|
||
<form method="POST" enctype="multipart/form-data" use:enhance> | ||
<FileInput | ||
name="track" | ||
label="Track" | ||
accept={acceptedExtensions} | ||
bind:value={$form.track} | ||
errors={$errors.track} | ||
/> | ||
<br /><progress max="100" value={progress}>{progress}%</progress> | ||
|
||
<div><button>Submit</button></div> | ||
</form> | ||
|
||
<hr /> | ||
<p><a target="_blank" href="https://superforms.rocks/api">API Reference</a></p> | ||
|
||
<style> | ||
.status { | ||
color: white; | ||
padding: 4px; | ||
padding-left: 8px; | ||
border-radius: 2px; | ||
font-weight: 500; | ||
} | ||
.status.success { | ||
background-color: seagreen; | ||
} | ||
.status.error { | ||
background-color: #ff2a02; | ||
} | ||
a { | ||
text-decoration: underline; | ||
} | ||
button { | ||
margin-top: 1rem; | ||
} | ||
hr { | ||
margin-top: 4rem; | ||
} | ||
form { | ||
padding-top: 1rem; | ||
padding-bottom: 1rem; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<script lang="ts"> | ||
import type { InputConstraint } from '$lib/index.js'; | ||
export let value: File | File[] | null; | ||
export let name: string; | ||
export let label: string | undefined = undefined; | ||
export let errors: string[] | undefined = undefined; | ||
export let constraints: InputConstraint | undefined = undefined; | ||
const input = (e: Event) => { | ||
const file = (e.currentTarget as HTMLInputElement).files?.item(0) ?? null; | ||
value = file; | ||
}; | ||
</script> | ||
|
||
<label for={name}>{label}</label> | ||
<input | ||
{name} | ||
id={name} | ||
type="file" | ||
on:change={input} | ||
aria-invalid={errors ? 'true' : undefined} | ||
{...constraints} | ||
{...$$restProps} | ||
/> | ||
{#if errors}<span class="error">{errors}</span>{/if} | ||
|
||
<style> | ||
.error { | ||
color: red; | ||
} | ||
input { | ||
margin-bottom: 0; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { z } from 'zod'; | ||
|
||
export const schema = z.object({ | ||
track: z.instanceof(File, { message: 'Please upload a file.' }) | ||
}); |