Skip to content

Commit

Permalink
fix: validators not firing with 'keepInvalid=true' when user input is…
Browse files Browse the repository at this point in the history
… well-formatted but date is invalid (outside allowed range). (#13133)
  • Loading branch information
Siggen authored Aug 26, 2023
1 parent a6a91b0 commit 5b72d58
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/app/components/calendar/calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/app/components/calendar/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2575,6 +2575,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
Expand Down

1 comment on commit 5b72d58

@vercel
Copy link

@vercel vercel bot commented on 5b72d58 Aug 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.