Skip to content

Commit 69168cc

Browse files
committed
Added checkbox onChange test
1 parent 2fe0cc4 commit 69168cc

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { zod } from '$lib/adapters/zod.js';
2+
import { message, 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+
return { form };
9+
};
10+
11+
export const actions = {
12+
default: async ({ request }) => {
13+
const formData = await request.formData();
14+
console.log(formData);
15+
16+
const form = await superValidate(formData, zod(schema));
17+
console.log(form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'Posted OK!');
22+
}
23+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
import CheckBox from './CheckBox.svelte';
5+
//import { zod } from '$lib/adapters/zod.js'
6+
//import { schema } from './schema.js';
7+
8+
export let data;
9+
10+
const theForm = superForm(data.form, {
11+
taintedMessage: false,
12+
onChange({ paths }) {
13+
console.log(paths);
14+
}
15+
//dataType: 'json',
16+
});
17+
18+
const { form, errors, tainted, message, enhance } = theForm;
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+
<div>Accept: <CheckBox form={theForm} field="accept" /></div>
27+
<div>
28+
<button>Submit</button>
29+
</div>
30+
</form>
31+
32+
<style lang="scss">
33+
form {
34+
margin: 2rem 0;
35+
36+
input {
37+
background-color: #dedede;
38+
}
39+
40+
.invalid {
41+
color: crimson;
42+
}
43+
}
44+
</style>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script lang="ts" context="module">
2+
type T = Record<string, unknown>;
3+
</script>
4+
5+
<script lang="ts" generics="T extends Record<string, unknown>">
6+
import { formFieldProxy, type SuperForm, type FormPathLeaves } from '$lib/index.js';
7+
import type { Writable } from 'svelte/store';
8+
9+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10+
export let form: SuperForm<T, any>;
11+
export let field: FormPathLeaves<T>;
12+
13+
const { value, constraints } = formFieldProxy(form, field);
14+
$: boolValue = value as Writable<boolean>;
15+
</script>
16+
17+
<input
18+
name={field}
19+
type="checkbox"
20+
class="checkbox"
21+
bind:checked={$boolValue}
22+
{...$constraints}
23+
{...$$restProps}
24+
/>
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+
accept: z.boolean()
5+
});

0 commit comments

Comments
 (0)