Skip to content

Commit d7cbd13

Browse files
committed
fix(material/datepicker): matDatepickerParse error not being added on first invalid value (#11524)
Fixes the datepicker not adding the `matDatepickerParse` error if the user enters an invalid string as their first value. The issue comes from the fact that we don't call the function from the `ControlValueAccessor` if the value hasn't changed, which means that the validator won't be re-run. Fixes #11523. (cherry picked from commit 09a906a)
1 parent 3e4a786 commit d7cbd13

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/material/datepicker/datepicker-input-base.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,12 @@ export abstract class MatDatepickerInputBase<S, D = ExtractDateTypeFromSelection
304304
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
305305
this._lastValueValid = this._isValidValue(date);
306306
date = this._dateAdapter.getValidDateOrNull(date);
307+
const hasChanged = !this._dateAdapter.sameDate(date, this.value);
307308

308-
if (!this._dateAdapter.sameDate(date, this.value)) {
309-
this._assignValue(date);
309+
// We need to fire the CVA change event for all
310+
// nulls, otherwise the validators won't run.
311+
if (!date || hasChanged) {
310312
this._cvaOnChange(date);
311-
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
312313
} else {
313314
// Call the CVA change handler for invalid values
314315
// since this is what marks the control as dirty.
@@ -320,6 +321,11 @@ export abstract class MatDatepickerInputBase<S, D = ExtractDateTypeFromSelection
320321
this._validatorOnChange();
321322
}
322323
}
324+
325+
if (hasChanged) {
326+
this._assignValue(date);
327+
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
328+
}
323329
}
324330

325331
_onChange() {

src/material/datepicker/datepicker.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
dispatchFakeEvent,
99
dispatchKeyboardEvent,
1010
dispatchMouseEvent,
11+
typeInElement,
1112
} from '../../cdk/testing/private';
1213
import {Component, Type, ViewChild, Provider, Directive, ViewEncapsulation} from '@angular/core';
1314
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
@@ -1048,6 +1049,17 @@ describe('MatDatepicker', () => {
10481049
subscription.unsubscribe();
10491050
}),
10501051
);
1052+
1053+
it('should set the matDatepickerParse error when an invalid value is typed for the first time', () => {
1054+
const formControl = fixture.componentInstance.formControl;
1055+
1056+
expect(formControl.hasError('matDatepickerParse')).toBe(false);
1057+
1058+
typeInElement(fixture.nativeElement.querySelector('input'), 'Today');
1059+
fixture.detectChanges();
1060+
1061+
expect(formControl.hasError('matDatepickerParse')).toBe(true);
1062+
});
10511063
});
10521064

10531065
describe('datepicker with mat-datepicker-toggle', () => {

0 commit comments

Comments
 (0)