From b7f3064a813a22dccb853fe920d87ad86a2d8595 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sat, 4 Nov 2023 16:34:55 -0700 Subject: [PATCH] Better display for custom field errors --- .../document-detail.component.html | 13 ++++++------- .../document-detail.component.spec.ts | 17 +++++++++++++++++ .../document-detail.component.ts | 5 +++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src-ui/src/app/components/document-detail/document-detail.component.html b/src-ui/src/app/components/document-detail/document-detail.component.html index b3465e28a..facd5ed4f 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.html +++ b/src-ui/src/app/components/document-detail/document-detail.component.html @@ -113,14 +113,13 @@
- - - - - - + + + + + - +
diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts index 23d6ce159..c0f1e44b7 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts @@ -879,6 +879,23 @@ describe('DocumentDetailComponent', () => { ) }) + it('should show custom field errors', () => { + initNormally() + component.error = { + custom_fields: [ + {}, + {}, + { value: ['This field may not be null.'] }, + {}, + { non_field_errors: ['Enter a valid URL.'] }, + ], + } + expect(component.getCustomFieldError(2)).toEqual([ + 'This field may not be null.', + ]) + expect(component.getCustomFieldError(4)).toEqual(['Enter a valid URL.']) + }) + function initNormally() { jest .spyOn(documentService, 'get') diff --git a/src-ui/src/app/components/document-detail/document-detail.component.ts b/src-ui/src/app/components/document-detail/document-detail.component.ts index 227c86411..368f1e632 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.ts @@ -859,6 +859,11 @@ export class DocumentDetailComponent return this.customFields?.find((f) => f.id === instance.field) } + public getCustomFieldError(index: number) { + const fieldError = this.error?.custom_fields?.[index] + return fieldError?.['non_field_errors'] ?? fieldError?.['value'] + } + private updateFormForCustomFields(emitEvent: boolean = false) { this.customFieldFormFields.clear({ emitEvent: false }) this.document.custom_fields?.forEach((fieldInstance) => {