From be7f3f5624f76ddcaa554805366622d7ba8dec4a Mon Sep 17 00:00:00 2001 From: pbardy2000 <146740183+pbardy2000@users.noreply.github.com> Date: Mon, 15 Jan 2024 09:20:14 +0000 Subject: [PATCH] feat(cb2-10112): custom un numbers validator (#1356) * feat(cb2-10112): allow addition of multiple UN numbers * fix(cb2-10211): fix duplicate error messages + global error service not focusing elements * feat(cb2-10032): better styling for readonly mode of tech records * feat(cb2-10032): add tc3 view mode component * feat(cb2-10032): tc2 inspections * feat(cb2-10032): remove validation for view control * fix(cb2-10174): update styling for edit component * feat(cb2-10032): remove expand/collapse notes temporarily * fix(cb2-10211): add appropriate warnings * fix(cb2-10211): fix interactions with global errors * fix(cb2-10211): update unit tests * fix(cb2-10211): disable add link when you cannot add the control * feat(cb2-10211): add custom view mode logic * feat(cb2-10211): add custom view * feat(cb2-10211): fix displaying of error messages * feat(cb2-10211): better UX * feat(cb2-10211): better form error displayed * feat(cb2-10211): only run validation when control is visible * feat(cb2-10112): Don't show global error message when first child is invalid, only show child errors * feat(cb2-10112): consistent spelling of UN number * feat(cb2-10112): correctly filter global error messages * feat(cb2-10112): custom un numbers validator --- ...tank-statement-un-number-edit.component.ts | 2 +- src/app/forms/models/validators.enum.ts | 1 + .../forms/services/dynamic-form.service.ts | 1 + .../forms/templates/general/adr.template.ts | 11 +------- src/app/forms/validators/custom-validators.ts | 26 +++++++++++++++++++ 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/app/forms/custom-sections/adr-tank-statement-un-number-edit/adr-tank-statement-un-number-edit.component.ts b/src/app/forms/custom-sections/adr-tank-statement-un-number-edit/adr-tank-statement-un-number-edit.component.ts index 8ef972497d..191024fabb 100644 --- a/src/app/forms/custom-sections/adr-tank-statement-un-number-edit/adr-tank-statement-un-number-edit.component.ts +++ b/src/app/forms/custom-sections/adr-tank-statement-un-number-edit/adr-tank-statement-un-number-edit.component.ts @@ -110,7 +110,7 @@ export class AdrTankStatementUnNumberEditComponent extends CustomFormControlComp .map((control) => ({ error: control.meta.customErrorMessage as string, anchorLink: control.meta.customId })); const allErrors = _.chain(globalErrors) - .filter((error) => Boolean(this.control?.hasError(error.error) && this.formArray.at(0).valid)) + .filter((error) => !(error.error === this.control?.meta.customErrorMessage && this.formArray.at(0).valid)) .concat(formErrors) .uniqBy((error) => error.error); diff --git a/src/app/forms/models/validators.enum.ts b/src/app/forms/models/validators.enum.ts index 9df7070ec2..deea8c548f 100644 --- a/src/app/forms/models/validators.enum.ts +++ b/src/app/forms/models/validators.enum.ts @@ -45,4 +45,5 @@ export enum ValidatorNames { Custom = 'custom', Tc3TestValidator = 'tc3TestValidator', DateIsInvalid = 'dateIsInvalid', + TankDetailsUnNumberValidator = 'tankDetailsUnNumberValidator', } diff --git a/src/app/forms/services/dynamic-form.service.ts b/src/app/forms/services/dynamic-form.service.ts index da352a4f07..602e30720a 100644 --- a/src/app/forms/services/dynamic-form.service.ts +++ b/src/app/forms/services/dynamic-form.service.ts @@ -87,6 +87,7 @@ export class DynamicFormService { [ValidatorNames.Tc3TestValidator]: (args: { inspectionNumber: number }) => CustomValidators.tc3TestValidator(args), [ValidatorNames.RequiredIfNotHidden]: () => CustomValidators.requiredIfNotHidden(), [ValidatorNames.DateIsInvalid]: () => CustomValidators.dateIsInvalid, + [ValidatorNames.TankDetailsUnNumberValidator]: () => CustomValidators.tankDetailsUnNumberValidator, }; // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/src/app/forms/templates/general/adr.template.ts b/src/app/forms/templates/general/adr.template.ts index ff027295cc..42e6f81108 100644 --- a/src/app/forms/templates/general/adr.template.ts +++ b/src/app/forms/templates/general/adr.template.ts @@ -494,16 +494,7 @@ export const AdrTemplate: FormNode = { name: ValidatorNames.RequiredIfEquals, args: { sibling: 'techRecord_adrDetails_tank_tankDetails_tankStatement_productListRefNo', value: [null, undefined, ''] }, }, - { - name: ValidatorNames.IsArray, - args: { - ofType: 'string', - whenEquals: { - sibling: 'techRecord_adrDetails_tank_tankDetails_tankStatement_select', - value: [ADRTankDetailsTankStatementSelect.PRODUCT_LIST], - }, - }, - }, + { name: ValidatorNames.TankDetailsUnNumberValidator }, ], }, { diff --git a/src/app/forms/validators/custom-validators.ts b/src/app/forms/validators/custom-validators.ts index fa63f7eb2c..0bf87b6048 100644 --- a/src/app/forms/validators/custom-validators.ts +++ b/src/app/forms/validators/custom-validators.ts @@ -569,6 +569,32 @@ export class CustomValidators { : null; }; }; + + /** + * Custom validator specifically for the UN numbers section of the ADR section of a tech record, applicable to: + * - HGVs, TRLs, or LGVs, + * - which carry dangerous goods, and + * - have an ADR body type which contains the words 'tank' or 'battery', and + * - have substances permitted selected with the 'class UN' option, and + * - have the subsequent 'product list' option selected from the subsequent select control + * + * The UN numbers form array is invalid if: + * - The above conditions are met, and + * - The reference number is empty and any UN number is the form array is empty + * + * @returns + */ + static tankDetailsUnNumberValidator = () => { + return (control: AbstractControl): ValidationErrors | null => { + if (control instanceof CustomFormControl) { + if (control.meta.hide) return null; + if (control.parent?.get('techRecord_adrDetails_tank_tankDetails_tankStatement_productListRefNo')?.value) return null; + return CustomValidators.isArray({ ofType: 'string' })(control); + } + + return null; + }; + }; } export type EnumValidatorOptions = {