Skip to content

Commit 4e34f40

Browse files
committed
id option for superForm wasn't used in multiple form scenarios.
1 parent 6faf42e commit 4e34f40

File tree

5 files changed

+129
-2
lines changed

5 files changed

+129
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
- `submit` method now falls back to submit, if no support for requestSubmit in browser.
1717
- `isTainted` now handles the type of `$tainted` in generic components.
18+
- `id` option for superForm wasn't used in multiple form scenarios.
1819

1920
## [2.12.2] - 2024-03-29
2021

src/lib/client/superForm.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,11 @@ export function superForm<
464464

465465
form = form as SuperValidated<T, M, In>;
466466

467-
// Check multiple id's
468-
const _initialFormId = options.id ?? form.id;
467+
// Assign options.id to form, if it exists
468+
const _initialFormId = (form.id = options.id ?? form.id);
469469
const _currentPage = get(page) ?? (STORYBOOK_MODE ? {} : undefined);
470470

471+
// Check multiple id's
471472
if (browser && options.warnings?.duplicateId !== false) {
472473
if (!formIds.has(_currentPage)) {
473474
formIds.set(_currentPage, new Set([_initialFormId]));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Actions, PageServerLoad } from './$types.js';
2+
3+
import { superValidate, message } from '$lib/index.js';
4+
import { zod } from '$lib/adapters/zod.js';
5+
import { fail } from '@sveltejs/kit';
6+
import { schema } from './schema.js';
7+
8+
export const load: PageServerLoad = async () => {
9+
return { form: await superValidate(zod(schema)) };
10+
};
11+
12+
export const actions: 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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<script lang="ts">
2+
import { zod, zodClient } from '$lib/adapters/zod.js';
3+
import { defaults, superForm } from '$lib/client/index.js';
4+
import SuperDebug from '$lib/client/SuperDebug.svelte';
5+
import { schema } from './schema.js';
6+
import { page } from '$app/stores';
7+
//import { zod } from '$lib/adapters/zod.js'
8+
//import { schema } from './schema.js';
9+
10+
export let data;
11+
12+
const { form, errors, tainted, message, enhance, formId } = superForm(data.form, {
13+
validators: zodClient(schema),
14+
taintedMessage: false,
15+
resetForm: true
16+
});
17+
18+
const {
19+
form: formData,
20+
errors: errors2,
21+
formId: formId2,
22+
enhance: enhance2,
23+
posted
24+
} = superForm(defaults(zod(schema)), {
25+
id: 'abc',
26+
taintedMessage: false,
27+
invalidateAll: false,
28+
resetForm: true,
29+
validators: zodClient(schema)
30+
});
31+
32+
// eslint-disable-next-line svelte/valid-compile
33+
$page;
34+
</script>
35+
36+
<SuperDebug data={{ $form, $errors, $tainted }} />
37+
38+
{JSON.stringify($page.form)}
39+
40+
{#if $message}<h4>{$message}</h4>{/if}
41+
42+
<form method="POST" use:enhance>
43+
<div>FORMID:{$formId}:</div>
44+
<div>POSTED:{$posted}:</div>
45+
<label>
46+
Name: <input
47+
name="name"
48+
bind:value={$form.name}
49+
aria-invalid={$errors.name ? 'true' : undefined}
50+
/>
51+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
52+
</label>
53+
<label>
54+
Email: <input
55+
name="email"
56+
bind:value={$form.email}
57+
aria-invalid={$errors.email ? 'true' : undefined}
58+
/>
59+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
60+
</label>
61+
<div>
62+
<button>Submit</button>
63+
</div>
64+
</form>
65+
66+
<form method="POST" use:enhance2>
67+
<div>FORMID:{$formId2}:</div>
68+
<div>POSTED:{$posted}:</div>
69+
<label>
70+
Name2: <input name="name" bind:value={$formData.name} />
71+
{#if $errors2.name}<span class="invalid">{$errors2.name}</span>{/if}
72+
</label>
73+
<label>
74+
Email: <input
75+
name="email"
76+
bind:value={$formData.email}
77+
aria-invalid={$errors2.email ? 'true' : undefined}
78+
/>
79+
{#if $errors2.email}<span class="invalid">{$errors2.email}</span>{/if}
80+
</label>
81+
<div>
82+
<button>Submit</button>
83+
</div>
84+
</form>
85+
86+
<style lang="scss">
87+
form {
88+
margin: 2rem 0;
89+
90+
input {
91+
background-color: #dedede;
92+
}
93+
94+
.invalid {
95+
color: crimson;
96+
}
97+
}
98+
</style>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
name: z.string().min(2),
5+
email: z.string()
6+
});

0 commit comments

Comments
 (0)