Skip to content

Commit eba864a

Browse files
committed
Fixed #196.
Nested array and object-level errors are now cleared after a successful client-side validation.
1 parent b3e8f77 commit eba864a

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
- Nested array and object-level errors are now all cleared on a successful client-side validation. ([#196](https://github.com/ciscoheat/sveltekit-superforms/issues/196))
1213
- Boolean fields with a default value of `true` always returned `true` when validating.
13-
- Fixed infinite deep type instantiation on `message`. (#143, thanks to [Alisson Cavalcante Agiani](https://github.com/thelinuxlich))
14-
- Fixed typesVersions map that caused incorrect auto-import paths (#191, thanks to [CokaKoala](https://github.com/AdrianGonz97))
14+
- Fixed infinite deep type instantiation on `message`. ([#143](https://github.com/ciscoheat/sveltekit-superforms/issues/143), thanks to [Alisson Cavalcante Agiani](https://github.com/thelinuxlich))
15+
- Fixed typesVersions map that caused incorrect auto-import paths ([#191](https://github.com/ciscoheat/sveltekit-superforms/pull/191), thanks to [CokaKoala](https://github.com/AdrianGonz97))
1516

1617
## [1.0.0]
1718

src/lib/client/clientValidation.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,9 @@ async function _validateField<T extends AnyZodObject, M>(
372372

373373
function Errors_clearFormLevelErrors() {
374374
Errors.update(($errors) => {
375-
$errors._errors = undefined;
375+
traversePaths($errors, (path) => {
376+
if (path.key == '_errors') return path.set(undefined);
377+
});
376378
return $errors;
377379
});
378380
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<script lang="ts">
2+
import { superForm, superValidateSync } from '$lib/client';
3+
import { z } from 'zod';
4+
5+
const childSchema = z.object({
6+
option: z.enum(['', 'one', 'two', 'three'])
7+
});
8+
const parentSchema = z.object({
9+
children: childSchema
10+
.array()
11+
.refine((children) =>
12+
children.some((child) => child.option.length > 0)
13+
)
14+
});
15+
16+
const initialValues = {
17+
children: [{ option: '' as const }, { option: '' as const }]
18+
};
19+
20+
const { enhance, form, errors, allErrors } = superForm(
21+
superValidateSync(initialValues, parentSchema),
22+
{
23+
SPA: true,
24+
dataType: 'json',
25+
validators: parentSchema,
26+
taintedMessage: null
27+
}
28+
);
29+
30+
$: console.log($form, $errors);
31+
</script>
32+
33+
{#if $errors.children?._errors}
34+
<p>{$errors.children._errors}</p>
35+
{/if}
36+
37+
<form method="POST" use:enhance>
38+
{#each $form.children as _child, i}
39+
<p>
40+
<label for={`options-${i}`}>Child {i + 1}</label>
41+
<select id={`options-${i}`} bind:value={$form.children[i].option}>
42+
<option value="">None</option>
43+
<option value="one">One</option>
44+
<option value="two">Two</option>
45+
<option value="three">Three</option>
46+
</select>
47+
</p>
48+
{/each}
49+
50+
<button type="submit" disabled={$allErrors?.length > 0}> Submit </button>
51+
</form>

0 commit comments

Comments
 (0)