I have two dates input - "start date" and "end date" - I also have two directives in which I am using as validators - the allowed minimum and the allowed maximum value for each field (so that the start date won't be later from the end date). If for example I change the start date to be later than the end date, the validator will alert that it is not valid. when I change the end date to a later date from the start date - this alert will not disappear since I haven't triggered the "customMax" validator. how can I trigger both validators at once on every change in one of the fields?
Thanks,
inputs HTML:
<input
type="text" class="form-control"
name="startDate{{d.index}}"
required
[customMax]="d.endDate"
(dateChange)="onDateChange('startDate', d.index, $event)"
[(ngModel)]="d.startDate"
appMyDatePicker>
<input type="text" class="form-control"
required
[customMin]="d.startDate"
name="endDate{{d.index}}"
(dateChange)="onDateChange('endDate', d.index, $event)"
[(ngModel)]="d.endDate"
appMyDatePicker>
customMax directive:
@Directive({
selector: '[appCustomMaxValidator],[customMax][ngModel]',
providers: [{provide: NG_VALIDATORS, useExisting:
CustomMaxValidatorDirective, multi: true}]
})
export class CustomMaxValidatorDirective implements Validator {
@Input()
customMax: Date;
constructor() { }
validate(c: FormControl): {[key: string]: any} {
const maxDateConvertInit = moment(this.customMax, 'DD/MM/YYYY HH:mm:ss').format('DD/MM/YYYY HH:mm:ss');
console.log('cant be greater than:' + maxDateConvertInit);
const maxDateConvertCompare = moment(c.value, 'DD/MM/YYYY HH:mm:ss').format('DD/MM/YYYY HH:mm:ss');
console.log('check date:' + maxDateConvertCompare);
const testScore = (maxDateConvertInit <= maxDateConvertCompare) ? {'customMax': true} : null;
return testScore;
}
}
customMin Directive:
@Directive({
selector: '[appCustomMinValidator],[customMin][ngModel]',
providers: [{provide: NG_VALIDATORS, useExisting: CustomMinValidatorDirective, multi: true}]
})
export class CustomMinValidatorDirective implements Validator {
@Input()
customMin: Date;
constructor() { }
validate(c: FormControl): {[key: string]: any} {
const minDateConvertInit = moment(this.customMin, 'DD/MM/YYYY HH:mm:ss').format('DD/MM/YYYY HH:mm:ss');
const minDateConvertCompare = moment(c.value, 'DD/MM/YYYY HH:mm:ss').format('DD/MM/YYYY HH:mm:ss');
const testScore = (minDateConvertInit >= minDateConvertCompare) ? {'customMin': true} : null;
return testScore;
}
}