Skip to content

fix(material/datepicker): date range input not marked as required when using reactive forms #26749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/material/datepicker/date-range-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
NG_VALIDATORS,
Validator,
NgModel,
Validators,
} from '@angular/forms';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {Directionality} from '@angular/cdk/bidi';
Expand Down Expand Up @@ -1111,6 +1112,31 @@ describe('MatDateRangeInput', () => {
.withContext('End date set three times')
.toBe(3);
}));

it('should mark the range picker as required when the entire group has the required validator', () => {
const fixture = createComponent(StandardRangePicker);
fixture.componentInstance.range = new FormGroup(
{
start: new FormControl<Date | null>(null),
end: new FormControl<Date | null>(null),
},
Validators.required,
);
fixture.detectChanges();

expect(fixture.componentInstance.rangeInput.required).toBe(true);
});

it('should mark the range picker as required when one part is required', () => {
const fixture = createComponent(StandardRangePicker);
fixture.componentInstance.range = new FormGroup({
start: new FormControl<Date | null>(null, Validators.required),
end: new FormControl<Date | null>(null),
});
fixture.detectChanges();

expect(fixture.componentInstance.rangeInput.required).toBe(true);
});
});

@Component({
Expand Down
17 changes: 14 additions & 3 deletions src/material/datepicker/date-range-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '@angular/core';
import {MatFormFieldControl, MAT_FORM_FIELD} from '@angular/material/form-field';
import {ThemePalette, DateAdapter} from '@angular/material/core';
import {NgControl, ControlContainer} from '@angular/forms';
import {NgControl, ControlContainer, Validators} from '@angular/forms';
import {Subject, merge, Subscription} from 'rxjs';
import {FocusOrigin} from '@angular/cdk/a11y';
import {coerceBooleanProperty, BooleanInput} from '@angular/cdk/coercion';
Expand Down Expand Up @@ -130,12 +130,18 @@ export class MatDateRangeInput<D>
/** Whether the input is required. */
@Input()
get required(): boolean {
return !!this._required;
return (
this._required ??
(this._isTargetRequired(this) ||
this._isTargetRequired(this._startInput) ||
this._isTargetRequired(this._endInput)) ??
false
);
}
set required(value: BooleanInput) {
this._required = coerceBooleanProperty(value);
}
private _required: boolean;
private _required: boolean | undefined;

/** Function that can be used to filter out dates within the date range picker. */
@Input()
Expand Down Expand Up @@ -422,4 +428,9 @@ export class MatDateRangeInput<D>
this._endInput._registerModel(model);
}
}

/** Checks whether a specific range input directive is required. */
private _isTargetRequired(target: {ngControl: NgControl | null} | null): boolean | undefined {
return target?.ngControl?.control?.hasValidator(Validators.required);
}
}