Skip to content

Commit

Permalink
[@mantine/form] Fix incorrect form.isValid behavior when nested fie…
Browse files Browse the repository at this point in the history
…ld has error (#3080)

Co-authored-by: Danny Gauthier <dgauthier@WKS-002169.corp.coveo.com>
  • Loading branch information
dmgauthier and Danny Gauthier committed Dec 4, 2022
1 parent b2310e1 commit 440b2e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/mantine-form/src/validate/validate-field-value.test.ts
Expand Up @@ -29,6 +29,16 @@ describe('@mantine/form/validate-field-value', () => {
).toStrictEqual({ hasError: true, error: 'error-b' });
});

it('validates parent of nested field with rules record', () => {
expect(
validateFieldValue(
'a',
{ a: { b: (value) => (value === 1 ? 'error-b' : null) } },
{ a: [{ b: 2 }, { b: 1 }] }
)
).toStrictEqual({ hasError: true, error: 'error-b' });
});

it('validates array field with rules record', () => {
expect(
validateFieldValue(
Expand Down
6 changes: 4 additions & 2 deletions src/mantine-form/src/validate/validate-field-value.ts
Expand Up @@ -11,6 +11,8 @@ export function validateFieldValue<T>(
}

const results = validateValues(rules, values);
const hasError = path in results.errors;
return { hasError, error: hasError ? results.errors[path] : null };
const pathInError = Object.keys(results.errors).find((errorKey) =>
path.split('.').every((pathPart, i) => pathPart === errorKey.split('.')[i])
);
return { hasError: !!pathInError, error: pathInError ? results.errors[pathInError] : null };
}

0 comments on commit 440b2e0

Please sign in to comment.