Skip to content

Commit cbc0c65

Browse files
committed
Array and form-level errors didn't respect submit-only as validationMethod.
1 parent c973bb2 commit cbc0c65

File tree

4 files changed

+146
-11
lines changed

4 files changed

+146
-11
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+
- Array and form-level errors didn't respect `submit-only` as validationMethod.
13+
814
## [1.7.1] - 2023-09-19
915

1016
### Fixed

src/lib/client/index.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,6 @@ export function superForm<
639639
path: string[],
640640
taint: TaintOption<UnwrappedT>
641641
) {
642-
if (
643-
options.validationMethod == 'onblur' ||
644-
options.validationMethod == 'submit-only'
645-
) {
646-
return false;
647-
}
648-
649642
let shouldValidate = options.validationMethod === 'oninput';
650643

651644
if (!shouldValidate) {
@@ -720,11 +713,21 @@ export function superForm<
720713
return tainted;
721714
});
722715

723-
let updated = false;
724-
for (const path of paths) {
725-
updated = updated || (await Tainted__validate(path, taintOptions));
716+
if (
717+
!(
718+
options.validationMethod == 'onblur' ||
719+
options.validationMethod == 'submit-only'
720+
)
721+
) {
722+
let updated = false;
723+
724+
for (const path of paths) {
725+
updated = updated || (await Tainted__validate(path, taintOptions));
726+
}
727+
if (!updated) {
728+
await validateObjectErrors(options, get(Form), Errors);
729+
}
726730
}
727-
if (!updated) await validateObjectErrors(options, get(Form), Errors);
728731
}
729732
}
730733

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { superValidate, message } from '$lib/server';
2+
import { fail } from '@sveltejs/kit';
3+
import { z } from 'zod';
4+
5+
import type { Actions, PageServerLoad } from './$types';
6+
7+
const schema = z.object({
8+
myString: z.string().min(10),
9+
myArray: z
10+
.number()
11+
.array()
12+
.default([-1])
13+
.refine((arg) => arg.every((n) => n >= 0), 'All numbers must >= 0')
14+
});
15+
16+
///// Load function /////
17+
18+
export const load: PageServerLoad = async () => {
19+
const form = await superValidate(schema);
20+
return { form };
21+
};
22+
23+
///// Form actions /////
24+
25+
export const actions: Actions = {
26+
default: async ({ request }) => {
27+
const form = await superValidate(request, schema);
28+
29+
console.log('POST', form);
30+
31+
if (!form.valid) return fail(400, { form });
32+
33+
return message(form, 'Form posted successfully!');
34+
}
35+
};
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { superForm } from '$lib/client';
4+
import { z } from 'zod';
5+
6+
export let data;
7+
8+
const { form, errors, message, enhance, allErrors } = superForm(
9+
data.form,
10+
{
11+
taintedMessage: null,
12+
validationMethod: 'submit-only',
13+
validators: z.object({
14+
myString: z.string().min(10),
15+
myArray: z
16+
.number()
17+
.array()
18+
.default([-1])
19+
.refine((arg) => arg.every((n) => n >= 0), 'All numbers must >= 0')
20+
})
21+
}
22+
);
23+
</script>
24+
25+
<h3>Submit-only validation</h3>
26+
27+
{#if $message}
28+
<div
29+
class="status"
30+
class:error={$page.status >= 400}
31+
class:success={$page.status == 200}
32+
>
33+
{$message}
34+
</div>
35+
{/if}
36+
37+
<p>
38+
{#each $allErrors as error}
39+
{error.path}: {error.messages}<br />
40+
{/each}
41+
</p>
42+
43+
<form method="POST" use:enhance>
44+
<label>
45+
My String<br />
46+
<input
47+
name="myString"
48+
aria-invalid={$errors.myString ? 'true' : undefined}
49+
bind:value={$form.myString}
50+
/>
51+
</label>
52+
53+
<!-- <label>
54+
My Array[0]<br />
55+
<input
56+
name="myArray[0]"
57+
type="text"
58+
aria-invalid={$errors.myArray ? 'true' : undefined}
59+
bind:value={$form.myArray[0]}
60+
/>
61+
</label> -->
62+
63+
<button>Submit</button>
64+
</form>
65+
66+
<style>
67+
.status {
68+
color: white;
69+
padding: 4px;
70+
padding-left: 8px;
71+
border-radius: 2px;
72+
font-weight: 500;
73+
}
74+
75+
.status.success {
76+
background-color: seagreen;
77+
}
78+
79+
.status.error {
80+
background-color: #ff2a02;
81+
}
82+
83+
input {
84+
background-color: #ddd;
85+
}
86+
87+
form {
88+
padding-top: 1rem;
89+
padding-bottom: 1rem;
90+
}
91+
</style>

0 commit comments

Comments
 (0)