Skip to content

Commit 524fa61

Browse files
committed
Added test for returning multiple forms.
1 parent d271bdc commit 524fa61

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { Actions, PageServerLoad } from './$types.js';
2+
3+
import { superValidate } from '$lib/index.js';
4+
import { zod } from '$lib/adapters/zod.js';
5+
import { fail } from '@sveltejs/kit';
6+
import { schema, other } from './schema.js';
7+
8+
export const load: PageServerLoad = async () => {
9+
const form = await superValidate(zod(schema));
10+
11+
// this does work
12+
// const otherForm = await superValidate({provider: "some provider"}, zod(other));
13+
const otherForm = await superValidate(zod(other));
14+
15+
return { form, otherForm };
16+
};
17+
18+
export const actions: Actions = {
19+
default: async ({ request }) => {
20+
const form = await superValidate(request, zod(schema));
21+
console.log(form);
22+
23+
if (!form.valid) return fail(400, { form });
24+
25+
const prefill = {
26+
provider: form.data.email.split('@').pop()
27+
};
28+
const otherForm = await superValidate(prefill, zod(other));
29+
30+
// return message(form, 'Form posted successfully!');
31+
return { form, otherForm };
32+
}
33+
};
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
6+
export let data;
7+
8+
console.dir(data, { depth: 10 }); //debug
9+
10+
const { form, errors, message, enhance } = superForm(data.form);
11+
const { form: otherForm, errors: otherErrors, enhance: otherEnhance } = superForm(data.otherForm);
12+
</script>
13+
14+
<SuperDebug data={$form} />
15+
16+
<h3>Fill in an email and the provider should be added.</h3>
17+
18+
{#if $message}
19+
<!-- eslint-disable-next-line svelte/valid-compile -->
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+
<label>
27+
Email<br />
28+
<input
29+
name="email"
30+
type="email"
31+
aria-invalid={$errors.email ? 'true' : undefined}
32+
bind:value={$form.email}
33+
/>
34+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
35+
</label>
36+
37+
<button>Submit</button>
38+
</form>
39+
40+
<hr />
41+
42+
<form method="POST" use:otherEnhance>
43+
<label>
44+
Provider<br />
45+
<input
46+
name="other"
47+
aria-invalid={$otherErrors.provider ? 'true' : undefined}
48+
bind:value={$otherForm.provider}
49+
/>
50+
{#if $otherErrors.provider}<span class="invalid">{$otherErrors.provider}</span>{/if}
51+
</label>
52+
53+
<button>Submit</button>
54+
</form>
55+
56+
<hr />
57+
<p>
58+
💥 <a target="_blank" href="https://superforms.rocks">Created with Superforms for SvelteKit</a> 💥
59+
</p>
60+
61+
<style>
62+
.invalid {
63+
color: red;
64+
}
65+
66+
.status {
67+
color: white;
68+
padding: 4px;
69+
padding-left: 8px;
70+
border-radius: 2px;
71+
font-weight: 500;
72+
}
73+
74+
.status.success {
75+
background-color: seagreen;
76+
}
77+
78+
.status.error {
79+
background-color: #ff2a02;
80+
}
81+
82+
input {
83+
background-color: #ddd;
84+
}
85+
86+
a {
87+
text-decoration: underline;
88+
}
89+
90+
hr {
91+
margin-top: 4rem;
92+
}
93+
94+
form {
95+
padding-top: 1rem;
96+
padding-bottom: 1rem;
97+
}
98+
</style>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
email: z.string().email()
5+
});
6+
7+
export const other = z.object({
8+
provider: z.string()
9+
});

0 commit comments

Comments
 (0)