I have a FormGroup that contains a nested DatePicker FormGroup; this nested FormGroup contains two FormControls FromDate and ToDate.
For this nested FormGroup Validators I have two of them. one depends on the each FormControl individually and one depends on the nested FormGroup as a whole.
The issue is that the Validators on the FormControls are not working only the Validator on the FormGroup as a whole is working.
here is a code snippet of my FormGroup
createForm() {
this.form = this.formBuilder.group({
id:[0],
dateFrom: [, Validators.required],
dateTo: [, Validators.required],
tempDateRange: new FormGroup({
from: new FormControl([, financialYearRangeValidator]),
to: new FormControl([, financialYearRangeValidator]),
}, periodRangeValidator),
});
}
I think I can make them all in one Validator but I thought it will be better separating them.
The
periodRangeValidatorbelong to the FormGrouptempDateRangeSo, if you're using simples datePickers (or inputs) you have
If you are using, material-angular, the mat-error is related to the input included in the mat-form-field so you need use a custom-error-matcher
A simple example:
1.-Define a class that implements ErrorStateMatcher
2.-In the component.ts
3.-In your .html
See a stackblitz
NOTE: See that it's a bit complex the custom error matcher because I want check if the two fields are not empty or null because if one have a Validators.required, the formGroup is invalid