Skip to content

Commit f971bc0

Browse files
committed
Added checkbox validation tests
1 parent 6af987e commit f971bc0

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { Actions, PageServerLoad } from './$types';
2+
import { superValidate } from '$lib/server';
3+
import { schema } from './schemas';
4+
5+
export const load = (async () => {
6+
const form = await superValidate(schema);
7+
return { form };
8+
}) satisfies PageServerLoad;
9+
10+
export const actions = {
11+
default: async ({ request }) => {
12+
const formData = await request.formData();
13+
const form = await superValidate(formData, schema);
14+
console.log('POST', form);
15+
16+
return { form };
17+
}
18+
} satisfies Actions;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 './schemas';
6+
import Checkbox from './Checkbox.svelte';
7+
8+
export let data: PageData;
9+
10+
const { form, errors, tainted, enhance } = superForm(data.form, {
11+
validators: schema,
12+
validationMethod: 'oninput'
13+
});
14+
</script>
15+
16+
<SuperDebug data={{ $form, $errors, $tainted }} />
17+
18+
<form method="POST" use:enhance>
19+
<Checkbox
20+
name="isAccredited"
21+
label="I confirm that I am an accredited investor"
22+
bind:checked={$form.isAccredited}
23+
/>
24+
{#if $errors.isAccredited}<span class="invalid"
25+
>{$errors.isAccredited}</span
26+
>{/if}
27+
<div>
28+
<button>Submit</button>
29+
</div>
30+
</form>
31+
32+
<style lang="scss">
33+
form {
34+
margin: 2rem 0;
35+
}
36+
37+
.invalid {
38+
color: red;
39+
}
40+
</style>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="ts">
2+
export let label: string;
3+
export let name: string;
4+
export let checked: boolean;
5+
</script>
6+
7+
<input type="checkbox" {name} bind:checked /> <span>{label}</span>
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+
isAccredited: z.literal(true)
5+
});

0 commit comments

Comments
 (0)