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

Commit 3c9ba38

Browse files
committed
fix(datepicker): apply ngMessages errors when using text input. Fixes
1 parent bd65bf7 commit 3c9ba38

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/components/datepicker/datePicker.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,19 @@
349349
* Sets the custom ngModel.$error flags to be consumed by ngMessages. Flags are:
350350
* - mindate: whether the selected date is before the minimum date.
351351
* - maxdate: whether the selected flag is after the maximum date.
352+
*
353+
* @param {Date=} opt_date Date to check. If not given, defaults to the datepicker's model value.
352354
*/
353-
DatePickerCtrl.prototype.setErrorFlags = function() {
354-
if (this.dateUtil.isValidDate(this.date)) {
355+
DatePickerCtrl.prototype.setErrorFlags = function(opt_date) {
356+
var date = opt_date || this.date;
357+
358+
if (this.dateUtil.isValidDate(date)) {
355359
if (this.dateUtil.isValidDate(this.minDate)) {
356-
this.ngModelCtrl.$setValidity('mindate', this.date >= this.minDate);
360+
this.ngModelCtrl.$setValidity('mindate', date >= this.minDate);
357361
}
358362

359363
if (this.dateUtil.isValidDate(this.maxDate)) {
360-
this.ngModelCtrl.$setValidity('maxdate', this.date <= this.maxDate);
364+
this.ngModelCtrl.$setValidity('maxdate', date <= this.maxDate);
361365
}
362366

363367
if (angular.isFunction(this.dateFilter)) {
@@ -388,9 +392,11 @@
388392
this.isDateEnabled(parsedDate)) {
389393
this.ngModelCtrl.$setViewValue(parsedDate);
390394
this.date = parsedDate;
395+
this.setErrorFlags();
391396
this.inputContainer.classList.remove(INVALID_CLASS);
392397
} else {
393398
// If there's an input string, it's an invalid date.
399+
this.setErrorFlags(parsedDate);
394400
this.inputContainer.classList.toggle(INVALID_CLASS, inputString);
395401
}
396402
};

src/components/datepicker/datePicker.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('md-date-picker', function() {
1616
'md-min-date="minDate" ' +
1717
'md-date-filter="dateFilter"' +
1818
'ng-model="myDate" ' +
19+
'ng-change="dateChangedHandler()" ' +
1920
'ng-required="isRequired" ' +
2021
'ng-disabled="isDisabled">' +
2122
'</md-datepicker>';
@@ -40,6 +41,7 @@ describe('md-date-picker', function() {
4041
pageScope = $rootScope.$new();
4142
pageScope.myDate = initialDate;
4243
pageScope.isDisabled = false;
44+
pageScope.dateChangedHandler = jasmine.createSpy('ng-change handler');
4345

4446
createDatepickerInstance(DATEPICKER_TEMPLATE);
4547
controller.closeCalendarPane();
@@ -224,6 +226,21 @@ describe('md-date-picker', function() {
224226
expect(controller.ngModelCtrl.$modelValue).toEqual(new Date(2014, JUN, 2));
225227
});
226228

229+
it('should apply ngMessages errors when the date changes from keyboard input', function() {
230+
pageScope.minDate = new Date(2014, JUN, 1);
231+
pageScope.$apply();
232+
233+
populateInputElement('5/30/2012');
234+
235+
expect(controller.ngModelCtrl.$error['mindate']).toBe(true);
236+
});
237+
238+
it('should evaluate ngChange expression when date changes from keyboard input', function() {
239+
populateInputElement('2/14/1976');
240+
241+
expect(pageScope.dateChangedHandler).toHaveBeenCalled();
242+
});
243+
227244
it('should add and remove the invalid class', function() {
228245
populateInputElement('6/1/2015');
229246
expect(controller.inputContainer).not.toHaveClass('md-datepicker-invalid');

0 commit comments

Comments
 (0)