Skip to content

Commit d8e8780

Browse files
committed
fix(datepicker): matDatepickerParse error not being added on first invalid value
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.
1 parent f2d2c19 commit d8e8780

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/material/datepicker/datepicker-input.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,21 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
325325
this._lastValueValid = !date || this._dateAdapter.isValid(date);
326326
date = this._getValidDateOrNull(date);
327327

328-
if (!this._dateAdapter.sameDate(date, this._value)) {
329-
this._value = date;
328+
const hasChanged = !this._dateAdapter.sameDate(date, this._value);
329+
330+
// We need to fire the CVA change event for all
331+
// nulls, otherwise the validators won't run.
332+
if (!date || hasChanged) {
330333
this._cvaOnChange(date);
331-
this._valueChange.emit(date);
332334
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
333335
} else if (lastValueWasValid !== this._lastValueValid) {
334336
this._validatorOnChange();
335337
}
338+
339+
if (hasChanged) {
340+
this._value = date;
341+
this._valueChange.emit(date);
342+
}
336343
}
337344

338345
_onChange() {

src/material/datepicker/datepicker.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
dispatchFakeEvent,
99
dispatchKeyboardEvent,
1010
dispatchMouseEvent,
11+
typeInElement,
1112
} from '@angular/cdk/testing/private';
1213
import {Component, Type, ViewChild, Provider} from '@angular/core';
1314
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
@@ -915,6 +916,18 @@ describe('MatDatepicker', () => {
915916
subscription.unsubscribe();
916917
}));
917918

919+
it('should set the matDatepickerParse error when an invalid value is ' +
920+
'typed for the first time', () => {
921+
const formControl = fixture.componentInstance.formControl;
922+
923+
expect(formControl.hasError('matDatepickerParse')).toBe(false);
924+
925+
typeInElement(fixture.nativeElement.querySelector('input'), 'Today');
926+
fixture.detectChanges();
927+
928+
expect(formControl.hasError('matDatepickerParse')).toBe(true);
929+
});
930+
918931
});
919932

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

0 commit comments

Comments
 (0)