Skip to content

Commit 3d9be46

Browse files
committed
Added checkbox crash test
1 parent f495162 commit 3d9be46

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

src/routes/(v2)/Navigation.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<li><a href="/v2/oninput-errors">oninput error display</a></li>
2626
<li><a href="/v2/multistep-client">Multi-step client</a></li>
2727
<li><a href="/v2/multistep-server">Multi-step server</a></li>
28+
<li><a href="/v2/issue-345">Checkbox crash</a></li>
2829
</ul>
2930
</nav>
3031

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { superValidate, message } from '$lib/index.js';
2+
import { zod } from '$lib/adapters/zod.js';
3+
import { fail } from '@sveltejs/kit';
4+
import { schema } from './schema.js';
5+
6+
export const load = async () => {
7+
console.log('Loading...');
8+
const form = await superValidate(zod(schema));
9+
return { form };
10+
};
11+
12+
export const actions = {
13+
default: async ({ request }) => {
14+
const form = await superValidate(request, zod(schema));
15+
console.log(form);
16+
17+
if (!form.valid) return fail(400, { form });
18+
19+
return message(form, 'Form posted successfully!');
20+
}
21+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { superForm } from '$lib/index.js';
4+
import SuperDebug from '$lib/index.js';
5+
import CheckboxComponent from './CheckboxComponent.svelte';
6+
import CheckboxField from './CheckboxField.svelte';
7+
8+
export let data;
9+
10+
const form = superForm(data.form, { taintedMessage: false });
11+
12+
const { form: formStore, errors, message, constraints, enhance } = form;
13+
</script>
14+
15+
<SuperDebug data={$formStore} />
16+
17+
<h3>Superforms testing ground - Zod</h3>
18+
19+
{#if $message}
20+
<div class="status" class:error={$page.status >= 400} class:success={$page.status == 200}>
21+
{$message}
22+
</div>
23+
{/if}
24+
25+
<form method="POST" use:enhance>
26+
<CheckboxComponent bind:checked={$formStore.checkbox} />
27+
28+
<!-- Comment the above line, uncomment this line, and reload to cause the failure on SSR (infinite loop). -->
29+
<!-- CheckboxField {form} field="checkbox" description="checkbox" / -->
30+
31+
<button>Submit</button>
32+
</form>
33+
34+
<hr />
35+
<p><a target="_blank" href="https://superforms.rocks/api">API Reference</a></p>
36+
37+
<style>
38+
.invalid {
39+
color: red;
40+
}
41+
42+
.status {
43+
color: white;
44+
padding: 4px;
45+
padding-left: 8px;
46+
border-radius: 2px;
47+
font-weight: 500;
48+
}
49+
50+
.status.success {
51+
background-color: seagreen;
52+
}
53+
54+
.status.error {
55+
background-color: #ff2a02;
56+
}
57+
58+
input {
59+
background-color: #ddd;
60+
}
61+
62+
a {
63+
text-decoration: underline;
64+
}
65+
66+
hr {
67+
margin-top: 4rem;
68+
}
69+
70+
form {
71+
padding-top: 1rem;
72+
padding-bottom: 1rem;
73+
}
74+
</style>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
export let checked = false;
3+
</script>
4+
5+
<input type="checkbox" bind:checked {...$$restProps} />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script lang="ts" context="module">
2+
type UnknownRecord = Record<string, unknown>;
3+
4+
type T = UnknownRecord;
5+
</script>
6+
7+
<script lang="ts" generics="T extends UnknownRecord">
8+
import type { Writable } from 'svelte/store';
9+
import type { FormPathLeaves } from '$lib/index.js';
10+
import { formFieldProxy, type SuperForm } from '$lib/index.js';
11+
import CheckboxComponent from './CheckboxComponent.svelte';
12+
export let form: SuperForm<T>;
13+
export let field: FormPathLeaves<T>;
14+
15+
const { value, errors } = formFieldProxy(form, field);
16+
$: boolValue = value as Writable<boolean>;
17+
</script>
18+
19+
<CheckboxComponent bind:checked={$boolValue} />
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+
checkbox: z.boolean().optional()
5+
});

0 commit comments

Comments
 (0)