Skip to content

Commit 7cd1800

Browse files
authored
fix(material/datepicker): date range input not marked as required when using reactive forms (#26749)
Fixes that the date range input wasn't styled as being required when using the `required` reactive forms validator. Fixes #26654.
1 parent 142e845 commit 7cd1800

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/material/datepicker/date-range-input.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
NG_VALIDATORS,
99
Validator,
1010
NgModel,
11+
Validators,
1112
} from '@angular/forms';
1213
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
1314
import {Directionality} from '@angular/cdk/bidi';
@@ -1111,6 +1112,31 @@ describe('MatDateRangeInput', () => {
11111112
.withContext('End date set three times')
11121113
.toBe(3);
11131114
}));
1115+
1116+
it('should mark the range picker as required when the entire group has the required validator', () => {
1117+
const fixture = createComponent(StandardRangePicker);
1118+
fixture.componentInstance.range = new FormGroup(
1119+
{
1120+
start: new FormControl<Date | null>(null),
1121+
end: new FormControl<Date | null>(null),
1122+
},
1123+
Validators.required,
1124+
);
1125+
fixture.detectChanges();
1126+
1127+
expect(fixture.componentInstance.rangeInput.required).toBe(true);
1128+
});
1129+
1130+
it('should mark the range picker as required when one part is required', () => {
1131+
const fixture = createComponent(StandardRangePicker);
1132+
fixture.componentInstance.range = new FormGroup({
1133+
start: new FormControl<Date | null>(null, Validators.required),
1134+
end: new FormControl<Date | null>(null),
1135+
});
1136+
fixture.detectChanges();
1137+
1138+
expect(fixture.componentInstance.rangeInput.required).toBe(true);
1139+
});
11141140
});
11151141

11161142
@Component({

src/material/datepicker/date-range-input.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
} from '@angular/core';
2525
import {MatFormFieldControl, MAT_FORM_FIELD} from '@angular/material/form-field';
2626
import {ThemePalette, DateAdapter} from '@angular/material/core';
27-
import {NgControl, ControlContainer} from '@angular/forms';
27+
import {NgControl, ControlContainer, Validators} from '@angular/forms';
2828
import {Subject, merge, Subscription} from 'rxjs';
2929
import {FocusOrigin} from '@angular/cdk/a11y';
3030
import {coerceBooleanProperty, BooleanInput} from '@angular/cdk/coercion';
@@ -130,12 +130,18 @@ export class MatDateRangeInput<D>
130130
/** Whether the input is required. */
131131
@Input()
132132
get required(): boolean {
133-
return !!this._required;
133+
return (
134+
this._required ??
135+
(this._isTargetRequired(this) ||
136+
this._isTargetRequired(this._startInput) ||
137+
this._isTargetRequired(this._endInput)) ??
138+
false
139+
);
134140
}
135141
set required(value: BooleanInput) {
136142
this._required = coerceBooleanProperty(value);
137143
}
138-
private _required: boolean;
144+
private _required: boolean | undefined;
139145

140146
/** Function that can be used to filter out dates within the date range picker. */
141147
@Input()
@@ -422,4 +428,9 @@ export class MatDateRangeInput<D>
422428
this._endInput._registerModel(model);
423429
}
424430
}
431+
432+
/** Checks whether a specific range input directive is required. */
433+
private _isTargetRequired(target: {ngControl: NgControl | null} | null): boolean | undefined {
434+
return target?.ngControl?.control?.hasValidator(Validators.required);
435+
}
425436
}

0 commit comments

Comments
 (0)