Skip to content

Commit 7c0f1bf

Browse files
committed
Respecting novalidate and formnovalidate.
1 parent 7efa2d0 commit 7c0f1bf

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- When cancelling a request, timers were cancelled too early in [SPA mode](https://superforms.rocks/concepts/spa) and when [client-side validation](https://superforms.rocks/concepts/client-validation) failed.
1717
- [Proxies](https://superforms.rocks/concepts/proxy-objects) didn't set or update a nested path unless it previously existed.
1818
- The when the [taint option](https://superforms.rocks/concepts/tainted#tainted-store) was set to `false`, client-side validation was prevented.
19+
- The `novalidate` and `formnovalidate` attributes on forms and buttons weren't respected.
1920

2021
## [1.10.2] - 2023-11-14
2122

src/lib/client/clientValidation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ export function validateForm<T extends AnyZodObject>(
7171
* Validate form data.
7272
*/
7373
export async function clientValidation<T extends AnyZodObject, M = unknown>(
74-
options: FormOptions<T, M>,
74+
validators: FormOptions<T, M>['validators'],
7575
checkData: z.infer<T>,
7676
formId: string | undefined,
7777
constraints: SuperValidated<ZodValidation<T>>['constraints'],
7878
posted: boolean
7979
) {
8080
return _clientValidation(
81-
options.validators,
81+
validators,
8282
checkData,
8383
formId,
8484
constraints,

src/lib/client/formEnhance.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,12 @@ export function formEnhance<T extends AnyZodObject, M>(
285285
} else {
286286
// Client validation
287287
const validation = await clientValidation(
288-
options,
288+
formEl.noValidate ||
289+
((submit.submitter instanceof HTMLButtonElement ||
290+
submit.submitter instanceof HTMLInputElement) &&
291+
submit.submitter.formNoValidate)
292+
? undefined
293+
: options.validators,
289294
get(data),
290295
get(formId),
291296
get(constraints),

src/lib/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ export function superForm<
919919
) {
920920
if (path === undefined) {
921921
return clientValidation<UnwrapEffects<T>, M>(
922-
options,
922+
options.validators,
923923
get(Form),
924924
_formId,
925925
get(Constraints),

src/routes/tests/array-component/+page.svelte

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<script lang="ts">
2-
import { arrayProxy, superForm } from '$lib/client';
2+
import { superForm } from '$lib/client';
33
import type { PageData } from './$types';
44
import SuperDebug from '$lib/client/SuperDebug.svelte';
55
import AutoComplete from './AutoComplete.svelte';
66
import { schema } from './schema';
77
88
export let data: PageData;
99
10+
let novalidate = false;
11+
1012
const pageForm = superForm(data.form, {
1113
dataType: 'json',
1214
validators: schema
@@ -23,10 +25,16 @@
2325

2426
{#if $message}<h4>{$message}</h4>{/if}
2527

26-
<form method="POST" use:enhance>
28+
<form {novalidate} method="POST" use:enhance>
2729
<AutoComplete form={pageForm} field="sub.tags" {options} taint={false} />
28-
<div>
30+
<div style="margin-top:1rem;">
2931
<button>Submit</button>
32+
<button formnovalidate>Submit formnovalidate</button>
33+
<input
34+
type="checkbox"
35+
bind:checked={novalidate}
36+
on:click={() => (novalidate = !novalidate)}
37+
/> novalidate on form
3038
</div>
3139
</form>
3240

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
{#if $message}<h4>{$message}</h4>{/if}
2525

26-
<form novalidate method="POST" use:enhance>
26+
<form method="POST" use:enhance>
2727
<label>
2828
Name: <input name="name" bind:value={$form.name} />
2929
</label>

0 commit comments

Comments
 (0)