Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 57c81c8

Browse files
Splaktarmmalerba
authored andcommitted
fix(datepicker): validation error when adding text after date (#11110)
## PR Checklist Please check that your PR fulfills the following requirements: - [x] The commit message follows [our guidelines](https://github.com/angular/material/blob/master/.github/CONTRIBUTING.md#-commit-message-format) - [x] Tests for the changes have been added (for bug fixes / features) - [x] Docs have been added / updated (for bug fixes / features) ## PR Type What kind of change does this PR introduce? ``` [x] Bugfix [ ] Feature [ ] Code style update (formatting, local variables) [ ] Refactoring (no functional changes, no api changes) [ ] Build related changes [ ] CI related changes [ ] Documentation content changes [ ] Infrastructure changes [ ] Other... Please describe: ``` ## What is the current behavior? this fixes a regression introduced in v1.1.0 against IE however the test is able to reproduce the issue on Chrome Issue Number: Fixes #9994. Closes #10520. Relates to #10015. ## What is the new behavior? The test passes and datepicker validation and ngMessages work properly on IE. ## Does this PR introduce a breaking change? ``` [ ] Yes [x] No ``` ## Other information thank you to Topher Fangio and Richard Hughes for the original solution
1 parent 7893772 commit 57c81c8

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/components/datepicker/js/datepickerDirective.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,31 @@
628628
this.ngModelCtrl.$setValidity('valid', date == null);
629629
}
630630

631+
var input = this.inputElement.value;
632+
var parsedDate = this.locale.parseDate(input);
633+
634+
if (!this.isInputValid(input, parsedDate) && this.ngModelCtrl.$valid) {
635+
this.ngModelCtrl.$setValidity('valid', date == null);
636+
}
637+
631638
angular.element(this.inputContainer).toggleClass(INVALID_CLASS, !this.ngModelCtrl.$valid);
632639
};
633640

641+
/**
642+
* Check to see if the input is valid as the validation should fail if the model is invalid
643+
*
644+
* @param {String} inputString
645+
* @param {Date} parsedDate
646+
* @return {boolean} Whether the input is valid
647+
*/
648+
DatePickerCtrl.prototype.isInputValid = function (inputString, parsedDate) {
649+
return inputString === '' || (
650+
this.dateUtil.isValidDate(parsedDate) &&
651+
this.locale.isDateComplete(inputString) &&
652+
this.isDateEnabled(parsedDate)
653+
);
654+
};
655+
634656
/** Clears any error flags set by `updateErrorState`. */
635657
DatePickerCtrl.prototype.clearErrorState = function() {
636658
this.inputContainer.classList.remove(INVALID_CLASS);
@@ -655,11 +677,7 @@
655677

656678
// An input string is valid if it is either empty (representing no date)
657679
// or if it parses to a valid date that the user is allowed to select.
658-
var isValidInput = inputString == '' || (
659-
this.dateUtil.isValidDate(parsedDate) &&
660-
this.locale.isDateComplete(inputString) &&
661-
this.isDateEnabled(parsedDate)
662-
);
680+
var isValidInput = this.isInputValid(inputString, parsedDate);
663681

664682
// The datepicker's model is only updated when there is a valid input.
665683
if (isValidInput) {

src/components/datepicker/js/datepickerDirective.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ describe('md-datepicker', function() {
348348
expect(controller.ngModelCtrl.$error['mindate']).toBe(true);
349349
});
350350

351+
it('should apply ngMessages errors when the date becomes invalid from keyboard input', function() {
352+
populateInputElement('5/30/2012');
353+
pageScope.$apply();
354+
expect(controller.ngModelCtrl.$error['valid']).toBeFalsy();
355+
356+
populateInputElement('5/30/2012z');
357+
pageScope.$apply();
358+
expect(controller.ngModelCtrl.$error['valid']).toBeTruthy();
359+
});
360+
351361
it('should evaluate ngChange expression when date changes from keyboard input', function() {
352362
populateInputElement('2/14/1976');
353363

0 commit comments

Comments
 (0)