Skip to content

Commit f9f3460

Browse files
committed
Added events test.
1 parent 20f1b21 commit f9f3460

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { message, superValidate } from '$lib/server';
2+
import { schema } from './schema';
3+
import { fail } from '@sveltejs/kit';
4+
5+
export const load = async () => {
6+
const form = await superValidate(schema);
7+
return { form };
8+
};
9+
10+
export const actions = {
11+
default: async ({ request }) => {
12+
const formData = await request.formData();
13+
console.log(formData);
14+
15+
const form = await superValidate(formData, schema);
16+
console.log('POST', form);
17+
18+
if (!form.valid) return fail(400, { form });
19+
20+
return message(form, 'Posted OK!');
21+
}
22+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client';
3+
import type { PageData } from './$types';
4+
import { schema } from './schema';
5+
import { page } from '$app/stores';
6+
7+
export let data: PageData;
8+
9+
let _log: string[] = [];
10+
let _cancel = false;
11+
12+
function log(msg: string) {
13+
_log = [..._log, msg];
14+
}
15+
16+
let SPA = false;
17+
$: options.SPA = SPA || undefined;
18+
19+
const { form, errors, message, enhance, options } = superForm(data.form, {
20+
taintedMessage: null,
21+
validators: schema,
22+
onSubmit: ({ cancel }) => {
23+
_log = ['onSubmit'];
24+
if (_cancel) {
25+
log('Cancelled');
26+
cancel();
27+
}
28+
},
29+
onError: () => {
30+
log('onError');
31+
},
32+
onResult: () => {
33+
log('onResult');
34+
},
35+
onUpdate: ({ form }) => {
36+
log('onUpdate');
37+
if (form.valid && SPA) form.message = 'SPA form OK!';
38+
},
39+
onUpdated: () => {
40+
log('onUpdated');
41+
}
42+
});
43+
</script>
44+
45+
<pre>{_log.join('\n')}</pre>
46+
47+
<input type="checkbox" bind:checked={SPA} /> SPA mode<br />
48+
<input type="checkbox" bind:checked={_cancel} /> Cancel submit in onSubmit
49+
50+
{#if $message}<h4>{$message}</h4>{/if}
51+
52+
<form method="POST" use:enhance>
53+
<label>
54+
Name: <input name="name" bind:value={$form.name} />
55+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
56+
</label>
57+
<div>
58+
<button>Submit</button>
59+
</div>
60+
</form>
61+
62+
<style lang="scss">
63+
form {
64+
margin: 2rem 0;
65+
66+
input {
67+
background-color: #dedede;
68+
}
69+
70+
.invalid {
71+
color: crimson;
72+
}
73+
}
74+
</style>

src/routes/tests/events/schema.ts

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)
5+
});

0 commit comments

Comments
 (0)