Skip to content

Commit bd401cf

Browse files
committed
Fixed clearOnSubmit.
1 parent a37abe5 commit bd401cf

File tree

6 files changed

+146
-18
lines changed

6 files changed

+146
-18
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+
### Changed
11+
12+
- The [clearOnSubmit](https://superforms.rocks/concepts/submit-behavior#clearonsubmit) option didn't clear the errors when supposed to. To avoid a breaking change, **the default option for clearOnSubmit is now** `message`, not `errors-and-message`, as it didn't work anyway.
13+
814
## [2.10.5] - 2024-03-18
915

1016
### Added

src/lib/client/superForm.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ const defaultFormOptions = {
349349
dataType: 'form',
350350
validators: undefined,
351351
customValidity: false,
352-
clearOnSubmit: 'errors-and-message',
352+
clearOnSubmit: 'message',
353353
delayMs: 500,
354354
timeoutMs: 8000,
355355
multipleSubmits: 'prevent',
@@ -1054,7 +1054,7 @@ export function superForm<
10541054
* To work with client-side validation, errors cannot be deleted but must
10551055
* be set to undefined, to know where they existed before (tainted+error check in oninput)
10561056
*/
1057-
clear: () => undefined
1057+
clear: () => Errors.set({})
10581058
};
10591059

10601060
//#endregion
@@ -1583,6 +1583,23 @@ export function superForm<
15831583
setTimeout(() => validationResponse({ result }), 0);
15841584
}
15851585

1586+
function clearOnSubmit() {
1587+
switch (options.clearOnSubmit) {
1588+
case 'errors-and-message':
1589+
Errors.clear();
1590+
Message.set(undefined);
1591+
break;
1592+
1593+
case 'errors':
1594+
Errors.clear();
1595+
break;
1596+
1597+
case 'message':
1598+
Message.set(undefined);
1599+
break;
1600+
}
1601+
}
1602+
15861603
function cancel(
15871604
opts: { resetTimers?: boolean } = {
15881605
resetTimers: true
@@ -1628,6 +1645,8 @@ export function superForm<
16281645
return await Form_validate({ adapter: validationAdapter });
16291646
};
16301647

1648+
clearOnSubmit();
1649+
16311650
if (!noValidate) {
16321651
validation = await validateForm();
16331652

@@ -1638,21 +1657,6 @@ export function superForm<
16381657
}
16391658

16401659
if (!cancelled) {
1641-
switch (options.clearOnSubmit) {
1642-
case 'errors-and-message':
1643-
Errors.clear();
1644-
Message.set(undefined);
1645-
break;
1646-
1647-
case 'errors':
1648-
Errors.clear();
1649-
break;
1650-
1651-
case 'message':
1652-
Message.set(undefined);
1653-
break;
1654-
}
1655-
16561660
if (
16571661
options.flashMessage &&
16581662
(options.clearOnSubmit == 'errors-and-message' || options.clearOnSubmit == 'message') &&

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
'file-component',
5454
'tainted-history',
5555
'files-proxy',
56-
'tainted-array'
56+
'tainted-array',
57+
'spa-clearonsubmit'
5758
].sort();
5859
</script>
5960

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { defaults, superForm } from '$lib/index.js';
4+
import SuperDebug from '$lib/index.js';
5+
import { valibot } from '$lib/adapters/valibot.js';
6+
import { schema } from './schema.js';
7+
8+
const data = defaults(valibot(schema));
9+
data.errors.name = ['Default error'];
10+
data.message = 'Submit form to remove this message.';
11+
12+
const { form, errors, message, enhance, submitting } = superForm(data, {
13+
SPA: true,
14+
validators: valibot(schema),
15+
clearOnSubmit: 'errors-and-message',
16+
async onUpdate({ form }) {
17+
// Slow external API request
18+
await new Promise((res) => setTimeout(res, 4000));
19+
if (form.valid) form.message = 'IT WORKS';
20+
}
21+
});
22+
</script>
23+
24+
<SuperDebug data={$form} />
25+
26+
<h3>SPA clear on submit</h3>
27+
28+
{#if $message}
29+
<!-- eslint-disable-next-line svelte/valid-compile -->
30+
<div class="status" class:error={$page.status >= 400} class:success={$page.status == 200}>
31+
{$message}
32+
</div>
33+
{/if}
34+
35+
<form method="POST" use:enhance>
36+
<label>
37+
Name<br />
38+
<input name="name" aria-invalid={$errors.name ? 'true' : undefined} bind:value={$form.name} />
39+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
40+
</label>
41+
42+
<label>
43+
Email<br />
44+
<input
45+
name="email"
46+
type="email"
47+
aria-invalid={$errors.email ? 'true' : undefined}
48+
bind:value={$form.email}
49+
/>
50+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
51+
</label>
52+
53+
<div class="submit">
54+
<button>Submit</button>
55+
{#if $submitting}Loading...{/if}
56+
</div>
57+
</form>
58+
59+
<hr />
60+
<p>
61+
💥 <a target="_blank" href="https://superforms.rocks">Created with Superforms for SvelteKit</a> 💥
62+
</p>
63+
64+
<style>
65+
.submit {
66+
display: flex;
67+
align-items: center;
68+
}
69+
70+
.submit button {
71+
margin: 0 0.5rem;
72+
}
73+
74+
.invalid {
75+
color: red;
76+
}
77+
78+
.status {
79+
color: white;
80+
padding: 4px;
81+
padding-left: 8px;
82+
border-radius: 2px;
83+
font-weight: 500;
84+
}
85+
86+
.status.success {
87+
background-color: seagreen;
88+
}
89+
90+
.status.error {
91+
background-color: #ff2a02;
92+
}
93+
94+
input {
95+
background-color: #ddd;
96+
}
97+
98+
a {
99+
text-decoration: underline;
100+
}
101+
102+
hr {
103+
margin-top: 4rem;
104+
}
105+
106+
form {
107+
padding-top: 1rem;
108+
padding-bottom: 1rem;
109+
}
110+
</style>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { object, string, email, minLength } from 'valibot';
2+
3+
export const schema = object({
4+
name: string([minLength(2)]),
5+
email: string([email()])
6+
});
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)