Skip to content

Commit 5aefe60

Browse files
mmalerbaandrewseguin
authored andcommitted
fix(datepicker): update validation when switching from null to error input (#14423)
* fix(datepicker): update validation when switching from null to error input * add test
1 parent 5a18ea6 commit 5aefe60

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/lib/datepicker/datepicker-input.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
329329
this._cvaOnChange(date);
330330
this._valueChange.emit(date);
331331
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
332+
} else {
333+
this._validatorOnChange();
332334
}
333335
}
334336

src/lib/datepicker/datepicker.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@angular/cdk/testing';
1212
import {Component, FactoryProvider, Type, ValueProvider, ViewChild} from '@angular/core';
1313
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
14-
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
14+
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
1515
import {MAT_DATE_LOCALE, MatNativeDateModule, NativeDateModule} from '@angular/material/core';
1616
import {MatFormField, MatFormFieldModule} from '@angular/material/form-field';
1717
import {DEC, JAN, JUL, JUN, SEP} from '@angular/material/testing';
@@ -1189,6 +1189,36 @@ describe('MatDatepicker', () => {
11891189
expect(fixture.debugElement.query(By.css('input')).nativeElement.classList)
11901190
.not.toContain('ng-invalid');
11911191
}));
1192+
1193+
it('should update validity when switching between null and invalid', fakeAsync(() => {
1194+
const inputEl = fixture.debugElement.query(By.css('input')).nativeElement;
1195+
inputEl.value = '';
1196+
dispatchFakeEvent(inputEl, 'input');
1197+
1198+
fixture.detectChanges();
1199+
flush();
1200+
fixture.detectChanges();
1201+
1202+
expect(testComponent.model.valid).toBe(true);
1203+
1204+
inputEl.value = 'abcdefg';
1205+
dispatchFakeEvent(inputEl, 'input');
1206+
1207+
fixture.detectChanges();
1208+
flush();
1209+
fixture.detectChanges();
1210+
1211+
expect(testComponent.model.valid).toBe(false);
1212+
1213+
inputEl.value = '';
1214+
dispatchFakeEvent(inputEl, 'input');
1215+
1216+
fixture.detectChanges();
1217+
flush();
1218+
fixture.detectChanges();
1219+
1220+
expect(testComponent.model.valid).toBe(true);
1221+
}));
11921222
});
11931223

11941224
describe('datepicker with filter and validation', () => {
@@ -1805,6 +1835,7 @@ class FormFieldDatepicker {
18051835
})
18061836
class DatepickerWithMinAndMaxValidation {
18071837
@ViewChild('d') datepicker: MatDatepicker<Date>;
1838+
@ViewChild(NgModel) model: NgModel;
18081839
date: Date | null;
18091840
minDate = new Date(2010, JAN, 1);
18101841
maxDate = new Date(2020, JAN, 1);

0 commit comments

Comments
 (0)