Skip to content

Commit

Permalink
feat(cb2-0000): implement validation for controls
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonT95 committed Nov 29, 2024
1 parent 7fe793c commit ef93d48
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1 tag>
</ng-container>
</h1>
</legend>
<ng-content></ng-content>
<div data-module="govuk-radios">
<div *ngFor="let option of options">
<input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,11 @@ <h1>
formControlName="techRecord_vehicleSize"
[options]="vehicleSizeOptions"
[tags]="[{ colour: TagType.RED, label: TagTypeLabels.REQUIRED }]"
></govuk-form-group-radio>
>
<div class="govuk-hint">
The Vehicle Size is calculated automatically based on the number of seats and standing capacity. Only change the Size if you need to
</div>
</govuk-form-group-radio>

<!-- Number of Seatbelts -->
<div class="govuk-form-group-input" *ngIf="shouldDisplayFormControl('techRecord_numberOfSeatbelts')">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { TagType } from '@components/tag/tag.component';
import { EUVehicleCategory } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/euVehicleCategory.enum.js';
import { EUVehicleCategory as HGVCategories } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/euVehicleCategoryHgv.enum.js';
import { EUVehicleCategory as PSVCategories } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/euVehicleCategoryPsv.enum.js';
import { VehicleClassDescription } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/vehicleClassDescription.enum';
import { FuelPropulsionSystem } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/hgv/complete';
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type';
import { getOptionsFromEnum } from '@forms/utils/enum-map';
Expand All @@ -29,6 +30,7 @@ import {
FrameDescriptions,
FuelTypes,
V3TechRecordModel,
VehicleSizes,
VehicleSubclass,
VehicleTypes,
} from '@models/vehicle-tech-record.model';
Expand Down Expand Up @@ -64,11 +66,11 @@ export class VehicleSectionEditComponent implements OnInit, OnDestroy {
// base properties that belong to all vehicle types
techRecord_euVehicleCategory: this.fb.control<string | null>(null),
techRecord_manufactureYear: this.fb.control<number | null>(null, [
this.commonValidators.max(9999, 'Year of manufacture must be less than or equal to 9999'),
this.commonValidators.min(1000, 'Year of manufacture must be greater than or equal to 1000'),
this.commonValidators.pastYear('Year of manufacture must be the current or a past year'),
]),
techRecord_statusCode: this.fb.control<string | null>(null),
techRecord_vehicleConfiguration: this.fb.control<VehicleConfiguration | null>(null, [this.updateFunctionCode]),
techRecord_vehicleType: this.fb.control<VehicleTypes | null>({ value: null, disabled: true }),
},
{ validators: [] }
Expand Down Expand Up @@ -122,6 +124,7 @@ export class VehicleSectionEditComponent implements OnInit, OnDestroy {
techRecord_alterationMarker: this.fb.control<boolean | null>(null),
techRecord_departmentalVehicleMarker: this.fb.control<boolean | null>(null),
techRecord_drawbarCouplingFitted: this.fb.control<boolean | null>(null),
techRecord_vehicleConfiguration: this.fb.control<VehicleConfiguration | null>(null, [this.updateFunctionCode]),
techRecord_emissionsLimit: this.fb.control<number | null>(null, [
this.commonValidators.max(99, 'Emission limit (m-1) (plate value) must be less than or equal to 99'),
this.commonValidators.pattern(/^\d*(\.\d{0,5})?$/, 'Emission limit (m-1) (plate value) Max 5 decimal places'),
Expand All @@ -147,6 +150,7 @@ export class VehicleSectionEditComponent implements OnInit, OnDestroy {
techRecord_euroStandard: this.fb.control<string | null>(null),
techRecord_alterationMarker: this.fb.control<boolean | null>(null),
techRecord_departmentalVehicleMarker: this.fb.control<boolean | null>(null),
techRecord_vehicleConfiguration: this.fb.control<VehicleConfiguration | null>(null, [this.updateFunctionCode]),
techRecord_emissionsLimit: this.fb.control<number | null>(null, [
this.commonValidators.max(99, 'Emission limit (m-1) (plate value) must be less than or equal to 99'),
this.commonValidators.pattern(/^\d*(\.\d{0,5})?$/, 'Emission limit (m-1) (plate value) Max 5 decimal places'),
Expand All @@ -156,13 +160,16 @@ export class VehicleSectionEditComponent implements OnInit, OnDestroy {
this.commonValidators.required('Vehicle class is required'),
]),
techRecord_seatsUpperDeck: this.fb.control<number | null>(null, [
this.commonValidators.required('Upper deck is required'),
this.commonValidators.max(99, 'Upper deck must be less than or equal to 99'),
this.handlePsvPassengersChange,
]),
techRecord_seatsLowerDeck: this.fb.control<number | null>(null, [
this.commonValidators.required('Lower deck is required'),
this.commonValidators.max(999, 'Lower deck must be less than or equal to 999'),
this.handlePsvPassengersChange,
]),
techRecord_standingCapacity: this.fb.control<number | null>(null, [
this.commonValidators.required('Standing capacity is required'),
this.commonValidators.max(999, 'Standing capacity must be less than or equal to 999'),
this.handlePsvPassengersChange,
]),
techRecord_vehicleSize: this.fb.control<string | null>(null, [
this.commonValidators.required('Vehicle size is required'),
Expand All @@ -188,12 +195,17 @@ export class VehicleSectionEditComponent implements OnInit, OnDestroy {
techRecord_roadFriendly: this.fb.control<boolean | null>(null),
techRecord_firstUseDate: this.fb.control<string | null>(null),
techRecord_suspensionType: this.fb.control<string | null>(null),
techRecord_couplingType: this.fb.control<string | null>(null),
techRecord_vehicleConfiguration: this.fb.control<VehicleConfiguration | null>(null, [this.updateFunctionCode]),
techRecord_couplingType: this.fb.control<string | null>(null, [
this.commonValidators.maxLength(1, 'Coupling type (optional) must be less than or equal to 1 characters'),
]),
techRecord_maxLoadOnCoupling: this.fb.control<number | null>(null, [
this.commonValidators.max(99999, 'Max load on coupling (optional) must be less than or equal to 99999'),
]),
techRecord_frameDescription: this.fb.control<string | null>(null),
techRecord_regnDate: this.fb.control<string | null>(null),
techRecord_regnDate: this.fb.control<string | null>(null, [
this.commonValidators.pastDate('Date of first registration must be the current or a past year'),
]),
techRecord_noOfAxles: this.fb.control<number | null>({ value: null, disabled: true }),
techRecord_manufactureMonth: this.fb.control<string | null>(null, [
this.commonValidators.max(9999, 'Year of manufacture must be less than or equal to 9999'),
Expand All @@ -209,6 +221,7 @@ export class VehicleSectionEditComponent implements OnInit, OnDestroy {
get smallTrlFields(): Partial<Record<any, FormControl>> {
return {
techRecord_vehicleSubclass: this.fb.control<string[] | null>(null),
techRecord_vehicleConfiguration: this.fb.control<VehicleConfiguration | null>(null),
techRecord_manufactureMonth: this.fb.control<string | null>(null, [
this.commonValidators.max(9999, 'Year of manufacture must be less than or equal to 9999'),
this.commonValidators.min(1000, 'Year of manufacture must be greater than or equal to 1000'),
Expand All @@ -220,27 +233,42 @@ export class VehicleSectionEditComponent implements OnInit, OnDestroy {
techRecord_vehicleClass_description: this.fb.control<string | null>(null, [
this.commonValidators.required('Vehicle class is required'),
]),
techRecord_noOfAxles: this.fb.control<number | null>(null),
techRecord_noOfAxles: this.fb.control<number | null>(null, [
this.commonValidators.max(99, 'Number of axles must be less than or equal to 99'),
]),
};
}

get lgvAndCarFields(): Partial<Record<keyof TechRecordType<'lgv' | 'car'>, FormControl>> {
return {
techRecord_vehicleSubclass: this.fb.control<string[] | null>(null),
techRecord_regnDate: this.fb.control<string | null>(null),
techRecord_noOfAxles: this.fb.control<number | null>({ value: null, disabled: true }),
techRecord_vehicleConfiguration: this.fb.control<VehicleConfiguration | null>(null, [
this.commonValidators.required('Vehicle configuration is required'),
]),
techRecord_noOfAxles: this.fb.control<number | null>(null, [
this.commonValidators.max(99, 'Number of axles must be less than or equal to 99'),
]),
};
}

// currently typed as string due to wrong typing of motorcycle, as it has a skeleton car in its place
// get motorcycleFields(): Partial<Record<keyof TechRecordType<'motorcycle'>, FormControl>> {
get motorcycleFields(): Partial<Record<string, FormControl>> {
return {
techRecord_numberOfWheelsDriven: this.fb.control<number | null>(null),
techRecord_numberOfWheelsDriven: this.fb.control<number | null>(null, [
this.commonValidators.max(9999, 'Number of wheels driven must be less than or equal to 9999'),
]),
techRecord_vehicleClass_description: this.fb.control<string | null>(null, [
this.commonValidators.required('Vehicle class is required'),
]),
techRecord_vehicleConfiguration: this.fb.control<VehicleConfiguration | null>(null, [
this.commonValidators.required('Vehicle configuration is required'),
]),
techRecord_regnDate: this.fb.control<string | null>(null),
techRecord_noOfAxles: this.fb.control<number | null>({ value: null, disabled: true }),
techRecord_noOfAxles: this.fb.control<number | null>(null, [
this.commonValidators.max(99, 'Number of axles must be less than or equal to 99'),
]),
};
}

Expand All @@ -263,6 +291,35 @@ export class VehicleSectionEditComponent implements OnInit, OnDestroy {
};
}

handlePsvPassengersChange(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (control.dirty) {
const seatsUpper: number = control.parent?.get('techRecord_seatsUpperDeck')?.value;
const seatsLower: number = control.parent?.get('techRecord_seatsLowerDeck')?.value;
const standingCapacity: number = control.parent?.get('techRecord_standingCapacity')?.value;

const classControl = control.parent?.get('techRecord_vehicleClass_description');
const sizeControl = control.parent?.get('techRecord_vehicleSize');

const totalPassengers = seatsUpper + seatsLower + standingCapacity;

switch (true) {
case totalPassengers <= 22: {
sizeControl?.setValue(VehicleSizes.SMALL, { emitEvent: false });
classControl?.setValue(VehicleClassDescription.SmallPsvIeLessThanOrEqualTo22Seats, { emitEvent: false });
break;
}
default: {
sizeControl?.setValue(VehicleSizes.LARGE, { emitEvent: false });
classControl?.setValue(VehicleClassDescription.LargePsvIeGreaterThan23Seats, { emitEvent: false });
}
}
control.markAsPristine();
}
return null;
};
}

get EUCategoryOptions() {
switch (this.techRecord()?.techRecord_vehicleType.toLowerCase()) {
case VehicleTypes.HGV:
Expand Down

0 comments on commit ef93d48

Please sign in to comment.