Skip to content

Commit 640753c

Browse files
committed
customRequest didn't cancel when client-side validation failed.
1 parent f04803a commit 640753c

File tree

5 files changed

+119
-2
lines changed

5 files changed

+119
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
- Support for Vine 2.0.
1818

19+
### Fixed
20+
21+
- [customRequest](https://superforms.rocks/concepts/events#customrequest) didn't cancel when client-side validation failed.
22+
1923
## [2.20.0] - 2024-10-18
2024

2125
### Added

src/lib/client/superForm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,8 +2044,8 @@ export function superForm<
20442044
unsubCheckforNav();
20452045
}
20462046

2047-
if (customRequest) {
2048-
if (!cancelled) _submitCancel();
2047+
if (!cancelled && customRequest) {
2048+
_submitCancel();
20492049
const response = await customRequest(submitParams);
20502050

20512051
let result: ActionResult;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { superValidate } 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+
const form = await superValidate(zod(schema));
8+
return { form };
9+
};
10+
11+
export const actions = {
12+
default: async ({ request }) => {
13+
console.log('==================================================================');
14+
const formData = await request.formData();
15+
console.log(formData);
16+
17+
const form = await superValidate(formData, zod(schema));
18+
console.log(form);
19+
20+
form.message = 'Form posted!';
21+
22+
if (!form.valid) return fail(400, { form });
23+
24+
return { form };
25+
}
26+
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<script lang="ts">
2+
import { zod } from '$lib/adapters/zod.js';
3+
import { superForm } from '$lib/index.js';
4+
import SuperDebug from '$lib/index.js';
5+
import type { PageData } from './$types.js';
6+
import { schema } from './schema.js';
7+
8+
export let data: PageData;
9+
10+
const { form, errors, message, enhance } = superForm(data.form, {
11+
taintedMessage: null,
12+
validators: zod(schema),
13+
onSubmit({ customRequest }) {
14+
return customRequest(async (input) => {
15+
const formEntriesUrlEncoded = new URLSearchParams();
16+
for (const [key, value] of input.formData.entries()) {
17+
if (typeof value === 'string') {
18+
formEntriesUrlEncoded.append(key, value);
19+
}
20+
}
21+
22+
const response = await fetch(input.action, {
23+
method: 'POST',
24+
body: formEntriesUrlEncoded,
25+
headers: {
26+
'x-sveltekit-action': 'true',
27+
Accept: 'application/json',
28+
'Content-Type': 'application/x-www-form-urlencoded'
29+
},
30+
credentials: 'include'
31+
});
32+
33+
console.log('sent');
34+
35+
return response;
36+
});
37+
}
38+
});
39+
</script>
40+
41+
<SuperDebug data={{ $form, $errors }} />
42+
43+
<h2>customRequest check</h2>
44+
45+
{#if $message}<h4>{$message}</h4>{/if}
46+
47+
<form method="POST" use:enhance>
48+
<label>
49+
Name: <input
50+
name="name"
51+
bind:value={$form.name}
52+
aria-invalid={$errors.name ? 'true' : undefined}
53+
/>
54+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
55+
</label>
56+
<label>
57+
Email: <input
58+
name="email"
59+
bind:value={$form.email}
60+
aria-invalid={$errors.email ? 'true' : undefined}
61+
/>
62+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
63+
</label>
64+
<div>
65+
<button>Submit</button>
66+
</div>
67+
</form>
68+
69+
<style lang="scss">
70+
form {
71+
margin: 2rem 0;
72+
73+
input {
74+
background-color: #dedede;
75+
}
76+
77+
.invalid {
78+
color: crimson;
79+
}
80+
}
81+
</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().email()
6+
});

0 commit comments

Comments
 (0)