Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/components/datepicker/js/datepickerDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/components/datepicker/js/datepickerDirective.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down