From 3c1346dc0fdfd57d7b98aa6fb6c20be17484384e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B6derlund?= Date: Tue, 19 Mar 2024 08:27:46 +0100 Subject: [PATCH] Fixed event error path for objects and arrays --- src/lib/client/superForm.ts | 16 +++++++++------- src/routes/(v1)/tests/tainted-array/+page.svelte | 6 +++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/lib/client/superForm.ts b/src/lib/client/superForm.ts index 1389542b..4cfb66ce 100644 --- a/src/lib/client/superForm.ts +++ b/src/lib/client/superForm.ts @@ -840,12 +840,12 @@ export function superForm< currentPath.pop(); } + const joinedPath = currentPath.join('.'); + function addError() { //console.log('Adding error', `[${error.path.join('.')}]`, error.value); //debug setPaths(output, [error.path], error.value); - const joinedPath = currentPath.join('.'); - if (options.customValidity && isEventError && validity.has(joinedPath)) { const { el, message } = validity.get(joinedPath)!; @@ -859,10 +859,16 @@ export function superForm< if (force) return addError(); + const lastPath = error.path[error.path.length - 1]; + const isObjectError = lastPath == '_errors'; + const isEventError = error.value && paths.some((path) => { - return currentPath && path && currentPath.length > 0 && currentPath[0] == path[0]; + // If array/object, any part of the path can match. If not, exact match is required + return isObjectError + ? currentPath && path && currentPath.length > 0 && currentPath[0] == path[0] + : joinedPath == path.join('.'); }); if (isEventError && options.validationMethod == 'oninput') return addError(); @@ -890,10 +896,6 @@ export function superForm< return addError(); } - const lastPath = error.path[error.path.length - 1]; - const isObjectError = lastPath == '_errors'; - //const isErrorInArray = error.path.some((p) => /^\d+$/.test(String(p))); - if (isObjectError) { // New object errors should be displayed on blur events, // or the (parent) path is or has been tainted. diff --git a/src/routes/(v1)/tests/tainted-array/+page.svelte b/src/routes/(v1)/tests/tainted-array/+page.svelte index 21f03045..7e08cd74 100644 --- a/src/routes/(v1)/tests/tainted-array/+page.svelte +++ b/src/routes/(v1)/tests/tainted-array/+page.svelte @@ -33,7 +33,7 @@ -
+ {#each $form.people as _, i}
@@ -41,7 +41,7 @@ {#if $errors.people?.[i]?.firstName} -

{$errors.people[i].firstName}

+

{$errors.people[i].firstName}

{/if}
@@ -49,7 +49,7 @@ {#if $errors.people?.[i]?.lastName} -

{$errors.people[i].lastName}

+

{$errors.people[i].lastName}

{/if}