Skip to content

Commit 3c1346d

Browse files
committed
Fixed event error path for objects and arrays
1 parent 99765c4 commit 3c1346d

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/lib/client/superForm.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -840,12 +840,12 @@ export function superForm<
840840
currentPath.pop();
841841
}
842842

843+
const joinedPath = currentPath.join('.');
844+
843845
function addError() {
844846
//console.log('Adding error', `[${error.path.join('.')}]`, error.value); //debug
845847
setPaths(output, [error.path], error.value);
846848

847-
const joinedPath = currentPath.join('.');
848-
849849
if (options.customValidity && isEventError && validity.has(joinedPath)) {
850850
const { el, message } = validity.get(joinedPath)!;
851851

@@ -859,10 +859,16 @@ export function superForm<
859859

860860
if (force) return addError();
861861

862+
const lastPath = error.path[error.path.length - 1];
863+
const isObjectError = lastPath == '_errors';
864+
862865
const isEventError =
863866
error.value &&
864867
paths.some((path) => {
865-
return currentPath && path && currentPath.length > 0 && currentPath[0] == path[0];
868+
// If array/object, any part of the path can match. If not, exact match is required
869+
return isObjectError
870+
? currentPath && path && currentPath.length > 0 && currentPath[0] == path[0]
871+
: joinedPath == path.join('.');
866872
});
867873

868874
if (isEventError && options.validationMethod == 'oninput') return addError();
@@ -890,10 +896,6 @@ export function superForm<
890896
return addError();
891897
}
892898

893-
const lastPath = error.path[error.path.length - 1];
894-
const isObjectError = lastPath == '_errors';
895-
//const isErrorInArray = error.path.some((p) => /^\d+$/.test(String(p)));
896-
897899
if (isObjectError) {
898900
// New object errors should be displayed on blur events,
899901
// or the (parent) path is or has been tainted.

src/routes/(v1)/tests/tainted-array/+page.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@
3333

3434
<button on:click={addPerson}>Add Person</button>
3535

36-
<form use:enhance>
36+
<form method="POST" use:enhance>
3737
<!-- eslint-disable-next-line @typescript-eslint/no-unused-vars -->
3838
{#each $form.people as _, i}
3939
<div>
4040
<div>
4141
<label for="firstName">First Name</label>
4242
<input name="firstName" bind:value={$form.people[i].firstName} />
4343
{#if $errors.people?.[i]?.firstName}
44-
<p>{$errors.people[i].firstName}</p>
44+
<p id="error-1">{$errors.people[i].firstName}</p>
4545
{/if}
4646
</div>
4747

4848
<div>
4949
<label for="lastName">Last Name</label>
5050
<input name="lastName" bind:value={$form.people[i].lastName} />
5151
{#if $errors.people?.[i]?.lastName}
52-
<p>{$errors.people[i].lastName}</p>
52+
<p id="error-2">{$errors.people[i].lastName}</p>
5353
{/if}
5454
</div>
5555
</div>

0 commit comments

Comments
 (0)