Skip to content

Commit cc1dd2d

Browse files
committed
customValidity now works with select, textarea and button.
1 parent 1526725 commit cc1dd2d

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
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.5.3] - 2023-08-16
99

1010
### Fixed
1111

1212
- Array-level errors weren't typed correctly when changing the cardinality of an array field in the schema (for example with `nonempty`).
13+
- `customValidity` now works with `select`, `textarea` and `button`, not just `input`.
1314
- SuperDebug looks a bit better now when there is no css styling on the page.
1415

1516
## [1.5.2] - 2023-08-15

src/lib/client/formEnhance.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,15 @@ function setCustomValidityForm<T extends AnyZodObject, M>(
8181
formEl: HTMLFormElement,
8282
errors: SuperValidated<ZodValidation<T>, M>['errors']
8383
) {
84-
for (const el of formEl.querySelectorAll('input')) {
85-
if (noCustomValidityDataAttribute in el.dataset) continue;
84+
for (const el of formEl.querySelectorAll<
85+
HTMLInputElement &
86+
HTMLSelectElement &
87+
HTMLTextAreaElement &
88+
HTMLButtonElement
89+
>('input,select,textarea,button')) {
90+
if (noCustomValidityDataAttribute in el.dataset) {
91+
continue;
92+
}
8693

8794
const error = traversePath(errors, splitPath(el.name));
8895
setCustomValidity(el, error?.value);
@@ -181,6 +188,10 @@ export function formEnhance<T extends AnyZodObject, M>(
181188
}
182189

183190
for (const change of get(lastChanges)) {
191+
console.log(
192+
'🚀 ~ file: formEnhance.ts:184 ~ checkBlur ~ change:',
193+
change
194+
);
184195
let validityEl: HTMLElement | null = null;
185196

186197
if (options.customValidity) {

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,39 @@
4141
</label>
4242

4343
<label>
44-
Accept terms: <input
45-
type="checkbox"
46-
name="accept"
47-
bind:checked={$form.accept}
48-
/>
44+
<select name="menu" bind:value={$form.menu}>
45+
<option value="">Select an option</option>
46+
<option value="first">First</option>
47+
<option value="second">Second</option>
48+
<option value="third">Third</option>
49+
</select>
50+
{#if $errors.menu}<span class="invalid">{$errors.menu}</span>{/if}
51+
</label>
52+
53+
<label>
54+
Radio:<br />
55+
<input name="radio" type="radio" bind:group={$form.radio} value={0} />
56+
0<br />
57+
<input name="radio" type="radio" bind:group={$form.radio} value={1} />
58+
1<br />
59+
<input name="radio" type="radio" bind:group={$form.radio} value={2} />
60+
2
61+
62+
<label>
63+
Text: <textarea name="text" bind:value={$form.text} />
64+
</label>
65+
66+
<label>
67+
Accept terms: <input
68+
type="checkbox"
69+
name="accept"
70+
bind:checked={$form.accept}
71+
/>
72+
</label>
73+
<div>
74+
<button>Submit</button>
75+
</div>
4976
</label>
50-
<div>
51-
<button>Submit</button>
52-
</div>
5377
</form>
5478

5579
<!--SuperDebug data={{ $form, $errors }} /-->

src/routes/tests/custom-validity/schema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ export const schema = z.object({
55
email: z.string().email(),
66
number: z.number().min(10),
77
info: z.string().min(1),
8+
menu: z.enum(['first', 'second', 'third']).default('' as any),
9+
radio: z.number().min(1),
10+
text: z.string().min(1),
811
accept: z.literal(true).default(false as true)
912
});

0 commit comments

Comments
 (0)