Skip to content

Commit 725caae

Browse files
committed
Added test for goto redirect in onUpdated
1 parent 0c26d63 commit 725caae

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { zod } from '$lib/adapters/zod.js';
2+
import { superValidate } from '$lib/server/index.js';
3+
import { schema } from './schema.js';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = async () => {
7+
const form = await superValidate(zod(schema));
8+
9+
const { data } = form;
10+
11+
return { form: data };
12+
};
13+
14+
export const actions = {
15+
default: async ({ request }) => {
16+
const form = await superValidate(request, zod(schema));
17+
18+
if (!form.valid) {
19+
return fail(400, {
20+
form
21+
});
22+
}
23+
24+
return { form };
25+
}
26+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<script lang="ts">
2+
import { goto } from '$app/navigation';
3+
import { zodClient } from '$lib/adapters/zod.js';
4+
import { superForm } from '$lib/client/index.js';
5+
import SuperDebug from '$lib/client/SuperDebug.svelte';
6+
import { schema } from './schema.js';
7+
8+
export let data;
9+
10+
const { form, errors, tainted, message, enhance } = superForm(data.form, {
11+
dataType: 'json',
12+
validators: zodClient(schema),
13+
onUpdated: ({ form }) => {
14+
if (form.valid) {
15+
goto('/v2/goto-onupdated/thanks');
16+
}
17+
}
18+
});
19+
</script>
20+
21+
<SuperDebug data={{ $form, $errors, $tainted }} />
22+
23+
{#if $message}<h4>{$message}</h4>{/if}
24+
25+
<form method="POST" use:enhance>
26+
<label>
27+
Name: <input
28+
name="name"
29+
bind:value={$form.name}
30+
aria-invalid={$errors.name ? 'true' : undefined}
31+
/>
32+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
33+
</label>
34+
<label>
35+
Email: <input
36+
name="email"
37+
bind:value={$form.email}
38+
aria-invalid={$errors.email ? 'true' : undefined}
39+
/>
40+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
41+
</label>
42+
<div>
43+
<button>Submit</button>
44+
</div>
45+
</form>
46+
47+
<style lang="scss">
48+
form {
49+
margin: 2rem 0;
50+
51+
input {
52+
background-color: #dedede;
53+
}
54+
55+
.invalid {
56+
color: crimson;
57+
}
58+
}
59+
</style>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
name: z.string().min(2),
5+
email: z.string().email()
6+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Thanks!</h1>

0 commit comments

Comments
 (0)