From 21d67aa6703496859d4677eccf820b411f5e2fdf Mon Sep 17 00:00:00 2001 From: Manuel Siggen Date: Fri, 2 Jun 2023 08:23:35 +0200 Subject: [PATCH] fix: validators not firing with 'keepInvalid=true' when user input is well-formatted but date is invalid (outside allowed range). --- src/app/components/calendar/calendar.spec.ts | 27 ++++++++++++++++++++ src/app/components/calendar/calendar.ts | 2 ++ 2 files changed, 29 insertions(+) diff --git a/src/app/components/calendar/calendar.spec.ts b/src/app/components/calendar/calendar.spec.ts index ad01043ef87..927c3800061 100755 --- a/src/app/components/calendar/calendar.spec.ts +++ b/src/app/components/calendar/calendar.spec.ts @@ -1398,6 +1398,33 @@ describe('Calendar', () => { expect(onModelTouchedSpy).toHaveBeenCalled(); }); + it('should trigger onModelChange when user input a well-formatted but invalid value with keepInvalid=true', () => { + + const today = new Date(); + + calendar.keepInvalid = true; + calendar.dateFormat = 'yy/mm/dd'; + calendar.minDate = today; + fixture.detectChanges(); + + const onModelChangeSpy = spyOn(calendar, 'onModelChange').and.callThrough(); + + const inputEl = fixture.debugElement.query(By.css('.p-inputtext')); + const focusEvent = new Event('focus'); + const blurEvent = new Event('blur'); + + inputEl.nativeElement.click(); + inputEl.nativeElement.dispatchEvent(focusEvent); + fixture.detectChanges(); + expect(onModelChangeSpy).not.toHaveBeenCalled(); + + // simulate user input + inputEl.nativeElement.value = '2000/01/01'; // <-- an invalid value (= before today) + calendar.isKeydown = true; + calendar.onUserInput({ target: inputEl.nativeElement }); + expect(onModelChangeSpy).toHaveBeenCalled(); + }); + it('should change appendto', () => { calendar.appendTo = 'body'; const date = new Date(2017, 8, 23); diff --git a/src/app/components/calendar/calendar.ts b/src/app/components/calendar/calendar.ts index 5b73061000c..9d259ba8db8 100755 --- a/src/app/components/calendar/calendar.ts +++ b/src/app/components/calendar/calendar.ts @@ -2566,6 +2566,8 @@ export class Calendar implements OnInit, OnDestroy, ControlValueAccessor { if (this.isValidSelection(value)) { this.updateModel(value); this.updateUI(); + } else if (this.keepInvalid) { + this.updateModel(value); } } catch (err) { //invalid date