blob: 680220d6e96d97c8e758678ebd0d29046a3dea96 [file] [log] [blame]
/********************************************************************************
* Copyright © 2018 Mettenmeier GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import { ValidatorFn, AbstractControl } from '@angular/forms';
export class DatepickerValidator {
static dateRangeTo(otherControlName: string): ValidatorFn {
return (validTo: AbstractControl): { [key: string]: boolean } | null => {
const validFrom: AbstractControl = validTo.root.get(otherControlName);
if (validFrom && validFrom.value && validTo.value) {
validFrom.valueChanges.subscribe(() => {
validTo.updateValueAndValidity({ onlySelf: true });
});
const validFromValue = validFrom.value;
const validFromDateObj = new Date(validFromValue.year, validFromValue.month, validFromValue.day, 0, 0, 0, 0);
const validToValue = validTo.value;
const validToDateObj = new Date(validToValue.year, validToValue.month, validToValue.day, 0, 0, 0, 0);
if (validFromDateObj > validToDateObj) {
return { invalidDate: true };
}
}
return null;
};
}
}