Skip to content

Commit 68cec07

Browse files
committed
Timing issue in SPA mode displayed errors when pressing enter.
1 parent bf16374 commit 68cec07

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.7.4] - 2023-09-29
9+
10+
### Fixed
11+
12+
- Timing issue in [SPA mode](https://superforms.rocks/concepts/spa) displayed errors for valid data, when submitting a form by pressing enter.
13+
814
## [1.7.3] - 2023-09-28
915

1016
### Fixed

src/lib/client/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,13 @@ export function superForm<
833833
page.subscribe(async (pageUpdate) => {
834834
if (!options.applyAction) return;
835835

836+
// Strange timing issue in SPA mode forces a wait here,
837+
// otherwise errors will appear even if the form is valid
838+
// when pressing enter to submit the form (not when clicking a submit button!)
839+
if (options.SPA) {
840+
await new Promise((r) => setTimeout(r, 0));
841+
}
842+
836843
const untaint = pageUpdate.status >= 200 && pageUpdate.status < 300;
837844

838845
if (pageUpdate.form && typeof pageUpdate.form === 'object') {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
5+
export let data;
6+
7+
let status = '';
8+
9+
const { form, enhance, errors } = superForm(data.form, {
10+
SPA: true,
11+
resetForm: true,
12+
validators: {
13+
title: (value) =>
14+
value.trim().length >= 3
15+
? null
16+
: 'Bitte geben Sie mindestens 3 Zeichen ein'
17+
},
18+
onUpdate: ({ form }) => {
19+
if (form.valid) {
20+
// send data;
21+
status = 'OK';
22+
} else {
23+
status = 'Not valid';
24+
}
25+
}
26+
});
27+
</script>
28+
29+
<SuperDebug data={{ $form, $errors }} />
30+
31+
<h3>Superforms testing ground</h3>
32+
33+
<p>{status}</p>
34+
35+
<form method="POST" use:enhance>
36+
<label>
37+
Name<br />
38+
<input
39+
class="input"
40+
type="text"
41+
bind:value={$form.title}
42+
aria-invalid={errors ? 'true' : undefined}
43+
/>
44+
</label>
45+
{#if $errors.title}<div class="invalid">{$errors.title}</div>{/if}
46+
47+
<button>Submit</button>
48+
</form>
49+
50+
<hr />
51+
<p>
52+
<a target="_blank" href="https://superforms.rocks/api">API Reference</a>
53+
</p>
54+
55+
<style>
56+
.invalid {
57+
color: red;
58+
}
59+
60+
input {
61+
background-color: #ddd;
62+
}
63+
64+
a {
65+
text-decoration: underline;
66+
}
67+
68+
hr {
69+
margin-top: 4rem;
70+
}
71+
72+
form {
73+
padding-top: 1rem;
74+
padding-bottom: 1rem;
75+
}
76+
</style>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { z } from 'zod';
2+
import { superValidate } from '$lib/client';
3+
4+
const Schema = z.object({
5+
title: z.string().min(3)
6+
});
7+
8+
export const load = async () => {
9+
const form = await superValidate(Schema);
10+
return { form };
11+
};

0 commit comments

Comments
 (0)