Skip to content

Commit 74c9b08

Browse files
committed
Fixed setError usage in load function.
1 parent 4350f49 commit 74c9b08

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
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+
- Using [setError](https://superforms.rocks/concepts/error-handling#seterror) in the load function and navigating to the same page client-side removed the errors.
13+
814
## [2.17.0] - 2024-08-13
915

1016
### Deprecated

src/lib/client/superForm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ export function superForm<
14811481
initialForm.data = newForm.data as T;
14821482
}
14831483

1484-
const resetStatus = Form_shouldReset(true, true);
1484+
const resetStatus = Form_shouldReset(newForm.valid, true);
14851485

14861486
rebind({
14871487
form: newForm as SuperValidated<T, M, In>,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { setError, superValidate } from '$lib/index.js';
2+
import { zod } from '$lib/adapters/zod.js';
3+
import { Schema1, Schema2 } from './shared.js';
4+
5+
export async function load({ url }) {
6+
const triggerError = url.searchParams.has('error');
7+
const form1 = await superValidate(zod(Schema1));
8+
const form2 = await superValidate(zod(Schema2));
9+
10+
console.log('=====', url.toString());
11+
12+
console.log('🚀 ~ load ~ triggerError:', triggerError);
13+
14+
if (triggerError) {
15+
setError(form1, 'value1', 'bad value1');
16+
}
17+
18+
console.log('🚀 ~ load ~ form1:', form1);
19+
20+
return { form1, form2 };
21+
}
22+
23+
export const actions = {};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/index.js';
3+
4+
export let data;
5+
6+
const sForm1 = superForm(data.form1, { resetForm: true });
7+
8+
const { errors: errors1 } = sForm1;
9+
</script>
10+
11+
<p id="error">{$errors1.value1 || 'NO ERROR'}</p>
12+
13+
<br />
14+
<a href="/v2/issue-467?error" data-sveltekit-preload-data="tap">Trigger error</a>
15+
<br />
16+
<a href="/v2/issue-467" data-sveltekit-preload-data="tap">Remove error</a>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { object, string, z } from "zod"
2+
3+
export const Schema1 = object({
4+
value1: string().min(1)
5+
})
6+
7+
export type Schema1 = z.infer<typeof Schema1>
8+
9+
10+
export const Schema2 = object({
11+
value2: string().min(1)
12+
})
13+
14+
export type Schema2 = z.infer<typeof Schema2>

0 commit comments

Comments
 (0)