Skip to content

Commit

Permalink
feat(cb2-10112): custom un numbers validator (#1356)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pbardy2000 authored Jan 15, 2024
1 parent 0108554 commit be7f3f5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/app/forms/models/validators.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ export enum ValidatorNames {
Custom = 'custom',
Tc3TestValidator = 'tc3TestValidator',
DateIsInvalid = 'dateIsInvalid',
TankDetailsUnNumberValidator = 'tankDetailsUnNumberValidator',
}
1 change: 1 addition & 0 deletions src/app/forms/services/dynamic-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions src/app/forms/templates/general/adr.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
],
},
{
Expand Down
26 changes: 26 additions & 0 deletions src/app/forms/validators/custom-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit be7f3f5

Please sign in to comment.