From 463ba7a1ac6821e5550f8ad872d5139430874afc Mon Sep 17 00:00:00 2001 From: richardhughes Date: Mon, 14 Nov 2016 20:07:31 +0000 Subject: [PATCH 1/2] fixes the validation for date picker in IE --- .../datepicker/js/datepickerDirective.js | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/components/datepicker/js/datepickerDirective.js b/src/components/datepicker/js/datepickerDirective.js index 050e69771f..b663193dcc 100644 --- a/src/components/datepicker/js/datepickerDirective.js +++ b/src/components/datepicker/js/datepickerDirective.js @@ -607,9 +607,31 @@ this.ngModelCtrl.$setValidity('valid', date == null); } + var input = this.inputElement.value; + var parsedDate = this.locale.parseDate(input); + + if (!this.isInputValid(input, parsedDate) && this.ngModelCtrl.$valid) { + this.ngModelCtrl.$setValidity('valid', date == null); + } + angular.element(this.inputContainer).toggleClass(INVALID_CLASS, !this.ngModelCtrl.$valid); }; + /** + * Check to see if the input is valid as the validation should fail if the model is invalid + * + * @param {String} inputString + * @param {Date} parsedDate + * @return {boolean} Whether the input is valid + */ + DatePickerCtrl.prototype.isInputValid = function (inputString, parsedDate) { + return inputString == '' || ( + this.dateUtil.isValidDate(parsedDate) && + this.locale.isDateComplete(inputString) && + this.isDateEnabled(parsedDate) + ); + }; + /** Clears any error flags set by `updateErrorState`. */ DatePickerCtrl.prototype.clearErrorState = function() { this.inputContainer.classList.remove(INVALID_CLASS); @@ -634,11 +656,7 @@ // An input string is valid if it is either empty (representing no date) // or if it parses to a valid date that the user is allowed to select. - var isValidInput = inputString == '' || ( - this.dateUtil.isValidDate(parsedDate) && - this.locale.isDateComplete(inputString) && - this.isDateEnabled(parsedDate) - ); + var isValidInput = this.isInputValid(inputString, parsedDate); // The datepicker's model is only updated when there is a valid input. if (isValidInput) { From 3d9aa1eb937b42bc7b1acc1fd967a75119dfc71f Mon Sep 17 00:00:00 2001 From: Topher Fangio Date: Tue, 7 Mar 2017 16:39:25 -0600 Subject: [PATCH 2/2] Add test. --- .../datepicker/js/datepickerDirective.spec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/components/datepicker/js/datepickerDirective.spec.js b/src/components/datepicker/js/datepickerDirective.spec.js index 38b045b479..7019cc17d2 100644 --- a/src/components/datepicker/js/datepickerDirective.spec.js +++ b/src/components/datepicker/js/datepickerDirective.spec.js @@ -344,6 +344,16 @@ describe('md-datepicker', function() { expect(controller.ngModelCtrl.$error['mindate']).toBe(true); }); + it('should apply ngMessages errors when the date becomes invalid from keyboard input', function() { + populateInputElement('5/30/2012'); + pageScope.$apply(); + expect(controller.ngModelCtrl.$error['valid']).toBeFalsy(); + + populateInputElement('5/30/2012z'); + pageScope.$apply(); + expect(controller.ngModelCtrl.$error['valid']).toBeTruthy(); + }); + it('should evaluate ngChange expression when date changes from keyboard input', function() { populateInputElement('2/14/1976');