Skip to content

Commit dd3903b

Browse files
committed
Events added in enhance weren't cleaned up when the form was destroyed.
1 parent 891a74a commit dd3903b

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

CHANGELOG.md

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

2020
- File uploads required extra configuration for the valibot adapter, now it works directly.
21+
- Events added in `enhance` weren't cleaned up when the form was destroyed. Note that this could be deprecated in a future version. Please add events only when calling `superForm`.
2122

2223
## [2.16.1] - 2024-07-18
2324

src/lib/client/superForm.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,7 @@ export function superForm<
16021602
| ((input: Parameters<SubmitFunction>[0]) => Promise<Response | XMLHttpRequest>)
16031603
| undefined = undefined;
16041604

1605-
return kitEnhance(FormElement, async (submitParams) => {
1605+
const enhanced = kitEnhance(FormElement, async (submitParams) => {
16061606
let jsonData: Record<string, unknown> | undefined = undefined;
16071607
let validationAdapter = options.validators;
16081608
undefined;
@@ -1990,6 +1990,18 @@ export function superForm<
19901990

19911991
return validationResponse;
19921992
});
1993+
1994+
return {
1995+
destroy: () => {
1996+
// Remove only events added in enhance
1997+
for (const [name, events] of Object.entries(formEvents)) {
1998+
// @ts-expect-error formEvents and options have the same keys
1999+
formEvents[name] = events.filter((e) => e === options[name]);
2000+
}
2001+
2002+
enhanced.destroy();
2003+
}
2004+
};
19932005
}
19942006

19952007
function removeFiles(formData: T) {

src/routes/(v2)/v2/Navigation.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
'schemasafe-types',
6363
'discriminated-union',
6464
'simple-tainted',
65-
'validity-objects'
65+
'validity-objects',
66+
'issue-466'
6667
].sort();
6768
</script>
6869

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { superValidate } from '$lib/index.js';
2+
import { zod } from '$lib/adapters/zod.js';
3+
import { schema } from './schema.js';
4+
5+
export const load = async () => {
6+
const form = await superValidate(zod(schema));
7+
return {
8+
form
9+
};
10+
};
11+
12+
export const actions = {
13+
default: async ({ request }) => {
14+
const form = await superValidate(request, zod(schema));
15+
return {
16+
form
17+
};
18+
}
19+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/index.js';
3+
import { zodClient } from '$lib/adapters/zod.js';
4+
import { schema } from './schema.js';
5+
6+
export let data;
7+
8+
let toggle = false;
9+
let updatedCount = 0;
10+
let updateCount = 0;
11+
12+
const { enhance, form } = superForm(data.form, {
13+
validators: zodClient(schema),
14+
onUpdate: () => updateCount++
15+
});
16+
17+
const onUpdated = () => updatedCount++;
18+
</script>
19+
20+
<button on:click={() => (toggle = !toggle)}>Toggle</button>
21+
22+
{#if toggle}
23+
<form method="post" use:enhance={{ onUpdated }}>
24+
<input type="text" name="name" bind:value={$form.name} />
25+
<button type="submit">Submit</button>
26+
</form>
27+
{/if}
28+
29+
<hr />
30+
31+
<p>Update count : {updateCount}</p>
32+
<p>Updated count : {updatedCount}</p>
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(1),
5+
email: z.string().email()
6+
});

0 commit comments

Comments
 (0)