Skip to content

Commit 48001cd

Browse files
committed
Error set on the server with setError didn't show up when submitting the form with an enter keypress.
1 parent 474acdc commit 48001cd

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-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+
## [Unreleased]
9+
10+
### Fixed
11+
12+
- Error set on the server with `setError` didn't show up when submitting the form with an enter keypress.
13+
814
## [1.13.3] - 2024-01-14
915

1016
### Fixed

src/lib/client/formEnhance.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ export function formEnhance<T extends AnyZodObject, M>(
369369
? submit.formData
370370
: (submit as { data: FormData }).data;
371371

372+
// Prevent input/blur events to trigger client-side validation,
373+
// and accidentally removing errors set by setError
374+
lastChanges.set([]);
375+
372376
if (options.SPA) {
373377
cancel(false);
374378

src/routes/Navigation.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<li><a href="/tests/array-component">Array proxy</a></li>
3636
<li><a href="/tests/redirect-same-route">Redirect with delay</a></li>
3737
<li><a href="/tests/strict-mode">Strict mode</a></li>
38+
<li><a href="/tests/submit-enter">Submit with enter</a></li>
3839
</ul>
3940
</nav>
4041

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { superValidate, setError } from '$lib/server/index.js';
2+
import { schema } from './schema.js';
3+
4+
import type { Actions, PageServerLoad } from './$types.js';
5+
6+
///// Load function /////
7+
8+
export const load: PageServerLoad = async () => {
9+
const form = await superValidate(schema);
10+
return { form };
11+
};
12+
13+
///// Form actions /////
14+
15+
export const actions: Actions = {
16+
default: async ({ request }) => {
17+
const form = await superValidate(request, schema);
18+
19+
console.log('POST', form);
20+
21+
return setError(form, 'name', 'some error');
22+
}
23+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
import { schema } from './schema.js';
5+
6+
export let data;
7+
8+
const { form, errors, enhance } = superForm(data.form, {
9+
validators: schema,
10+
taintedMessage: false
11+
});
12+
</script>
13+
14+
<SuperDebug data={$form} />
15+
16+
<h3>Superforms testing ground</h3>
17+
18+
<p>
19+
Pressing enter and clicking submit should both display a server-side only
20+
error.
21+
</p>
22+
23+
<form method="POST" use:enhance>
24+
<label>
25+
Name<br />
26+
<input
27+
name="name"
28+
aria-invalid={$errors.name ? 'true' : undefined}
29+
bind:value={$form.name}
30+
/>
31+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
32+
</label>
33+
34+
<label>
35+
Password<br />
36+
<input
37+
name="password"
38+
type="password"
39+
aria-invalid={$errors.password ? 'true' : undefined}
40+
bind:value={$form.password}
41+
/>
42+
{#if $errors.password}<span class="invalid">{$errors.password}</span
43+
>{/if}
44+
</label>
45+
46+
<button>Submit</button>
47+
</form>
48+
49+
<hr />
50+
<p>
51+
<a target="_blank" href="https://superforms.rocks/api">API Reference</a>
52+
</p>
53+
54+
<style>
55+
.invalid {
56+
color: red;
57+
}
58+
59+
input {
60+
background-color: #ddd;
61+
}
62+
63+
a {
64+
text-decoration: underline;
65+
}
66+
67+
hr {
68+
margin-top: 4rem;
69+
}
70+
71+
form {
72+
padding-top: 1rem;
73+
padding-bottom: 1rem;
74+
}
75+
</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(1),
5+
password: z.string().min(1)
6+
});

0 commit comments

Comments
 (0)