Skip to content

Commit 567351f

Browse files
committed
Test case for redirecting to same route.
1 parent 7e4d02b commit 567351f

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { superValidate } from '$lib/server';
2+
import { redirect } from 'sveltekit-flash-message/server';
3+
import { schema } from './schema';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = async () => {
7+
const form = await superValidate(schema);
8+
return { form };
9+
};
10+
11+
export const actions = {
12+
default: async (event) => {
13+
await new Promise((r) => setTimeout(r, 1000));
14+
15+
const formData = await event.request.formData();
16+
console.log(formData);
17+
18+
const form = await superValidate(formData, schema);
19+
console.log('POST', form);
20+
21+
if (!form.valid) return fail(400, { form });
22+
23+
throw redirect(
24+
{ type: 'success', message: 'Redirect to same page' },
25+
event
26+
);
27+
}
28+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client';
3+
import type { PageData } from './$types';
4+
import SuperDebug from '$lib/client/SuperDebug.svelte';
5+
import { schema } from './schema';
6+
7+
export let data: PageData;
8+
9+
const { form, errors, tainted, message, enhance, submitting } = superForm(
10+
data.form,
11+
{
12+
//dataType: 'json',
13+
//validators: schema
14+
}
15+
);
16+
</script>
17+
18+
<SuperDebug data={{ $form, $errors, $tainted }} />
19+
20+
{#if $message}<h4>{$message}</h4>{/if}
21+
22+
<form method="POST" use:enhance>
23+
<label>
24+
Name: <input name="name" bind:value={$form.name} />
25+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
26+
</label>
27+
<div class="toolbar">
28+
<button>Submit</button>
29+
{#if $submitting}
30+
<svg
31+
xmlns="http://www.w3.org/2000/svg"
32+
width="24"
33+
height="24"
34+
viewBox="0 0 24 24"
35+
><path
36+
fill="currentColor"
37+
d="M12 21a9 9 0 1 1 6.18-15.55a.75.75 0 0 1 0 1.06a.74.74 0 0 1-1.06 0A7.51 7.51 0 1 0 19.5 12a.75.75 0 0 1 1.5 0a9 9 0 0 1-9 9Z"
38+
/></svg
39+
>
40+
{/if}
41+
</div>
42+
</form>
43+
44+
<style lang="scss">
45+
.toolbar {
46+
display: flex;
47+
align-items: center;
48+
gap: 0.5rem;
49+
}
50+
51+
form {
52+
margin: 2rem 0;
53+
54+
input {
55+
background-color: #dedede;
56+
}
57+
58+
.invalid {
59+
color: crimson;
60+
}
61+
}
62+
</style>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
name: z.string().min(1).default('Test')
5+
});

0 commit comments

Comments
 (0)