From 5aefe60186c4a9096134c0b6ab42b0a507229c6a Mon Sep 17 00:00:00 2001 From: mmalerba Date: Fri, 25 Jan 2019 09:08:57 -0800 Subject: [PATCH] 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 --- src/lib/datepicker/datepicker-input.ts | 2 ++ src/lib/datepicker/datepicker.spec.ts | 33 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/lib/datepicker/datepicker-input.ts b/src/lib/datepicker/datepicker-input.ts index 6d48626d713..4ba62766c52 100644 --- a/src/lib/datepicker/datepicker-input.ts +++ b/src/lib/datepicker/datepicker-input.ts @@ -329,6 +329,8 @@ export class MatDatepickerInput implements ControlValueAccessor, OnDestroy, V this._cvaOnChange(date); this._valueChange.emit(date); this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement)); + } else { + this._validatorOnChange(); } } diff --git a/src/lib/datepicker/datepicker.spec.ts b/src/lib/datepicker/datepicker.spec.ts index e552e98b031..261a4a3a32d 100644 --- a/src/lib/datepicker/datepicker.spec.ts +++ b/src/lib/datepicker/datepicker.spec.ts @@ -11,7 +11,7 @@ import { } from '@angular/cdk/testing'; import {Component, FactoryProvider, Type, ValueProvider, ViewChild} from '@angular/core'; import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing'; -import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms'; +import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms'; import {MAT_DATE_LOCALE, MatNativeDateModule, NativeDateModule} from '@angular/material/core'; import {MatFormField, MatFormFieldModule} from '@angular/material/form-field'; import {DEC, JAN, JUL, JUN, SEP} from '@angular/material/testing'; @@ -1189,6 +1189,36 @@ describe('MatDatepicker', () => { expect(fixture.debugElement.query(By.css('input')).nativeElement.classList) .not.toContain('ng-invalid'); })); + + it('should update validity when switching between null and invalid', fakeAsync(() => { + const inputEl = fixture.debugElement.query(By.css('input')).nativeElement; + inputEl.value = ''; + dispatchFakeEvent(inputEl, 'input'); + + fixture.detectChanges(); + flush(); + fixture.detectChanges(); + + expect(testComponent.model.valid).toBe(true); + + inputEl.value = 'abcdefg'; + dispatchFakeEvent(inputEl, 'input'); + + fixture.detectChanges(); + flush(); + fixture.detectChanges(); + + expect(testComponent.model.valid).toBe(false); + + inputEl.value = ''; + dispatchFakeEvent(inputEl, 'input'); + + fixture.detectChanges(); + flush(); + fixture.detectChanges(); + + expect(testComponent.model.valid).toBe(true); + })); }); describe('datepicker with filter and validation', () => { @@ -1805,6 +1835,7 @@ class FormFieldDatepicker { }) class DatepickerWithMinAndMaxValidation { @ViewChild('d') datepicker: MatDatepicker; + @ViewChild(NgModel) model: NgModel; date: Date | null; minDate = new Date(2010, JAN, 1); maxDate = new Date(2020, JAN, 1);