Skip to content

Commit 313e60a

Browse files
committed
LIBDRUM-909. Force re-display of validation errors on page refresh
These changes are an attempt to fix an apparent race condition between the Angular field validation and the DSpace refresh of the page on attempted submission. The issue being fixed is that the GUI validation error warnings momentarily "flash" and then disappear when submitting the form using the "Deposit" button. The workflow appears to be: * The user does not focus on or enter anything in a required field, and then attempts to submit the form. * The request to the DSpace backend does not include any JSON related to the required field * A 200 response is returned from the DSpace backend, with an "errors" array in the JSON, indicating the validation error * The 200 response triggers a page refresh, which wipes out any existing validation error display on the page. This change forces all the validation errors to be re-displayed by touching the relevant fields and triggering a validity check after the page is refreshed. It is unclear whether this is the optimal fix, so it has not been submitted upstream to DSpace. Also, DSpace has been working on various fixes, so the changes in this class should be re-evaluated on upgrades, and removed if possible. https://umd-dit.atlassian.net/browse/LIBDRUM-909
1 parent 5388c7b commit 313e60a

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

docs/DrumAngularCustomizations.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,21 @@ would successfully complete:
8080

8181
* Commented out the "codecov" job, because UMD does not have an appropriate key
8282
for uploading the results to codecov.io.
83+
84+
## Change to Submission Form Validation Handling
85+
86+
The "SubmissionSectionFormComponent" class
87+
(src/app/submission/sections/form/section-form.component.ts) has been
88+
modified with a "ngAfterViewChecked" method to ensure that form validation
89+
errors are displayed, even after a page refresh.
90+
91+
This change is intended to fix an issue (see LIBDRUM-909) in which submitting
92+
a form without all the required fields populated would not show GUI validation
93+
warnings on all affected fields. This is apparently caused by a race condition
94+
between the Angular validation handling, and a page refresh triggered by the
95+
DSpace backend response.
96+
97+
It is unclear whether this is the optimal fix, so it has not been submitted
98+
upstream to DSpace. Also, DSpace has been working on various similar fixes, so
99+
the changes in this class should be re-evaluated on upgrades, and
100+
removed if possible.

src/app/submission/sections/form/section-form.component.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NgIf } from '@angular/common';
22
import {
3+
AfterViewChecked,
34
ChangeDetectorRef,
45
Component,
56
Inject,
@@ -82,7 +83,16 @@ import { SectionFormOperationsService } from './section-form-operations.service'
8283
],
8384
standalone: true,
8485
})
85-
export class SubmissionSectionFormComponent extends SectionModelComponent {
86+
// UMD Customization
87+
// This class is customized to correct an issue seem with the display of
88+
// validation errors (see LIBDRUM-909).
89+
//
90+
// It is unclear whether this is the optimal fix, so it has not been submitted
91+
// upstream to DSpace. Also, DSpace has been working on various fixes, so
92+
// the changes in this class should be re-evaluated on upgrades, and
93+
// removed if possible.
94+
export class SubmissionSectionFormComponent extends SectionModelComponent implements AfterViewChecked {
95+
// End UMD Customization
8696

8797
/**
8898
* The form id
@@ -157,6 +167,11 @@ export class SubmissionSectionFormComponent extends SectionModelComponent {
157167
*/
158168
protected isSectionReadonly = false;
159169

170+
// UMD Customization
171+
private triggerValidationErrorDisplay = false;
172+
private pendingErrors: SubmissionSectionError[] = [];
173+
// End UMD Customization
174+
160175
/**
161176
* The FormComponent reference
162177
*/
@@ -231,7 +246,57 @@ export class SubmissionSectionFormComponent extends SectionModelComponent {
231246
this.cdr.detectChanges();
232247
}
233248
});
249+
250+
251+
// UMD Customization
252+
// Logic to maintain red borders after an attempted deposit
253+
const sectionErrors = this.sectionService.getSectionErrors(this.submissionId, this.sectionData.id);
254+
if (sectionErrors) {
255+
this.subs.push(
256+
sectionErrors.pipe(
257+
filter((errors: SubmissionSectionError[]) => errors && errors.length > 0),
258+
).subscribe((errors: SubmissionSectionError[]) => {
259+
this.pendingErrors = errors;
260+
this.triggerValidationErrorDisplay = true;
261+
}),
262+
);
263+
}
264+
// End UMD Customization
265+
}
266+
267+
// UMD Customization
268+
ngAfterViewChecked() {
269+
if (this.triggerValidationErrorDisplay && this.formRef && this.formRef.formGroup) {
270+
// Lower the flag immediately to prevent infinite loops
271+
this.triggerValidationErrorDisplay = false;
272+
let touchedAny = false;
273+
this.pendingErrors.forEach((error: SubmissionSectionError) => {
274+
// Paths have the form /sections/mhheaDescribe/dc.title
275+
const pathParts = error.path.split('/');
276+
const fieldId = pathParts[pathParts.length - 1];
277+
const formGroup = this.formRef.formGroup;
278+
279+
if (formGroup) {
280+
// Retrieve the specific control from the Reactive Form
281+
const normalizedFieldId = fieldId.replace(/\./g, '_');
282+
const control = this.formBuilderService.getFormControlById(normalizedFieldId, formGroup, this.formModel);
283+
284+
if (control) {
285+
// markAsTouched() triggers the 'is-invalid' class/red border
286+
control.markAsTouched();
287+
// Optional: markAsDirty() ensures Angular treats the field as modified
288+
control.markAsDirty();
289+
control.updateValueAndValidity();
290+
touchedAny = true;
291+
}
292+
}
293+
});
294+
if (touchedAny) {
295+
this.cdr.detectChanges();
296+
}
297+
}
234298
}
299+
// End UMD Customization
235300

236301
/**
237302
* Unsubscribe from all subscriptions

0 commit comments

Comments
 (0)