fix: add date range validation translations for all languages (#2851)
This commit is contained in:
@@ -29,6 +29,9 @@
|
||||
{{ param.label }} {{ 'labels.commons.is' | translate }}
|
||||
<strong>{{ 'labels.commons.required' | translate }}</strong>
|
||||
</mat-error>
|
||||
<mat-error *ngIf="reportForm.controls[param.name].hasError('endBeforeStart')">
|
||||
{{ param.label }} {{ 'labels.commons.must be after or equal to start date' | translate }}
|
||||
</mat-error>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field class="form-field" *ngSwitchCase="'select'">
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
/** Angular Imports */
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { ActivatedRoute, RouterLink } from '@angular/router';
|
||||
import { UntypedFormControl, UntypedFormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
|
||||
import {
|
||||
AbstractControl,
|
||||
UntypedFormControl,
|
||||
UntypedFormGroup,
|
||||
ValidatorFn,
|
||||
Validators,
|
||||
ReactiveFormsModule
|
||||
} from '@angular/forms';
|
||||
|
||||
/** Custom Services */
|
||||
import { ReportsService } from '../reports.service';
|
||||
@@ -176,6 +183,7 @@ export class RunReportComponent implements OnInit {
|
||||
}
|
||||
this.decimalChoice.patchValue('0');
|
||||
this.setChildControls();
|
||||
this.addDateRangeValidator();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -206,6 +214,62 @@ export class RunReportComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
addDateRangeValidator(): void {
|
||||
const dateParams = this.paramData.filter((param: ReportParameter) => param.displayType === 'date');
|
||||
const startParam = dateParams.find((param: ReportParameter) => this.isStartDateParam(param));
|
||||
const endParam = dateParams.find((param: ReportParameter) => this.isEndDateParam(param));
|
||||
|
||||
if (!startParam || !endParam) {
|
||||
return;
|
||||
}
|
||||
|
||||
const startControl = this.reportForm.get(startParam.name);
|
||||
const endControl = this.reportForm.get(endParam.name);
|
||||
|
||||
if (!startControl || !endControl) {
|
||||
return;
|
||||
}
|
||||
|
||||
endControl.addValidators(this.endDateAfterStartValidator(startParam.name));
|
||||
endControl.updateValueAndValidity({ emitEvent: false });
|
||||
startControl.valueChanges.subscribe(() => endControl.updateValueAndValidity({ emitEvent: false }));
|
||||
}
|
||||
|
||||
endDateAfterStartValidator(startControlName: string): ValidatorFn {
|
||||
return (control: AbstractControl) => {
|
||||
const startControl = control.parent?.get(startControlName);
|
||||
const startValue = startControl?.value;
|
||||
const endValue = control.value;
|
||||
|
||||
if (!startValue || !endValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const startDate = new Date(startValue);
|
||||
const endDate = new Date(endValue);
|
||||
|
||||
if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (endDate < startDate) {
|
||||
return { endBeforeStart: true };
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
isStartDateParam(param: ReportParameter): boolean {
|
||||
const identifier = `${param.name}${param.variable}${param.label}`.toLowerCase();
|
||||
return identifier.includes('start') || identifier.includes('from');
|
||||
}
|
||||
|
||||
isEndDateParam(param: ReportParameter): boolean {
|
||||
const identifier = `${param.name}${param.variable}${param.label}`.toLowerCase();
|
||||
return identifier.includes('end') || identifier.includes('to');
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes to changes in parent parameters value, builds child parameter vis-a-vis parent's value.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user