Skip to content

Commit 8f1821c

Browse files
committed
customValidity fix for submit-only and onblur validationMethods.
1 parent cbc0c65 commit 8f1821c

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +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]
8+
## [1.7.2] - 2023-09-23
99

1010
### Fixed
1111

12-
- Array and form-level errors didn't respect `submit-only` as validationMethod.
12+
- Array and form-level errors didn't respect `submit-only` as [validationMethod](https://superforms.rocks/concepts/client-validation#validationmethod). ([#270](https://github.com/ciscoheat/sveltekit-superforms/issues/270))
13+
- The [customValidity](https://superforms.rocks/concepts/error-handling#customvalidity) option didn't respect `submit-only` and `onblur` as validationMethod.
1314

1415
## [1.7.1] - 2023-09-19
1516

src/lib/client/formEnhance.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,16 @@ export function formEnhance<T extends AnyZodObject, M>(
135135
event: 'blur' | 'input',
136136
validityEl: HTMLElement | null
137137
) {
138+
if (options.validationMethod == 'submit-only') return;
139+
138140
if (options.customValidity && validityEl) {
139141
// Always reset validity, in case it has been validated on the server.
140142
if ('setCustomValidity' in validityEl) {
141143
(validityEl as HTMLInputElement).setCustomValidity('');
142144
}
143145

146+
if (event == 'input' && options.validationMethod == 'onblur') return;
147+
144148
// If event is input but element shouldn't use custom validity,
145149
// return immediately since validateField don't have to be called
146150
// in this case, validation is happening elsewhere.
@@ -204,6 +208,13 @@ export function formEnhance<T extends AnyZodObject, M>(
204208

205209
// Add input event, for custom validity
206210
async function checkCustomValidity(e: Event) {
211+
if (
212+
options.validationMethod == 'onblur' ||
213+
options.validationMethod == 'submit-only'
214+
) {
215+
return;
216+
}
217+
207218
if (timingIssue(e.target)) {
208219
await new Promise((r) => setTimeout(r, 0));
209220
}
@@ -222,6 +233,7 @@ export function formEnhance<T extends AnyZodObject, M>(
222233
}
223234
}
224235
}
236+
225237
if (options.customValidity) {
226238
formEl.addEventListener('input', checkCustomValidity);
227239
}

src/routes/tests/custom-validity/+page.svelte

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
<script lang="ts">
2-
import { superForm } from '$lib/client';
32
import type { PageData } from './$types';
4-
import SuperDebug from '$lib/client/SuperDebug.svelte';
3+
4+
import { page } from '$app/stores';
5+
import { superForm } from '$lib/client';
56
import { schema } from './schema';
67
78
export let data: PageData;
89
9-
const { form, errors, tainted, message, enhance, constraints } = superForm(
10-
data.form,
11-
{
12-
customValidity: true,
13-
validators: schema
14-
}
15-
);
10+
const validationMethod =
11+
($page.url.searchParams.get('method') as any) ?? 'auto';
12+
13+
const { form, errors, message, enhance } = superForm(data.form, {
14+
customValidity: true,
15+
validators: schema,
16+
validationMethod,
17+
taintedMessage:
18+
validationMethod == 'auto'
19+
? 'You have unsaved changes. Leave page?'
20+
: null
21+
});
1622
</script>
1723

1824
{#if $message}<h4>{$message}</h4>{/if}
1925

20-
<form method="POST" use:enhance>
26+
<form novalidate method="POST" use:enhance>
2127
<label>
2228
Name: <input name="name" bind:value={$form.name} />
2329
</label>
@@ -76,6 +82,10 @@
7682
</label>
7783
</form>
7884

85+
{#if validationMethod != 'auto'}
86+
<p>Validation method: <b>{validationMethod}</b></p>
87+
{/if}
88+
7989
<!--SuperDebug data={{ $form, $errors }} /-->
8090

8191
<style lang="scss">

0 commit comments

Comments
 (0)