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

Commit 11f65e3

Browse files
committed
fix(datepicker): update error state only after $validate has run
- only decorate with `md-datepicker-invalid` class (red underline) - if the control has been `$touched` or `$submitted` - this improves the behavior when the `ng-model` value changes programmatically Fixes #10360
1 parent 3d5ff5d commit 11f65e3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/components/datepicker/js/datepickerDirective.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,8 @@
664664
this.ngModelCtrl.$setValidity('valid', date == null);
665665
}
666666

667-
angular.element(this.inputContainer).toggleClass(INVALID_CLASS, !this.ngModelCtrl.$valid);
667+
angular.element(this.inputContainer).toggleClass(INVALID_CLASS,
668+
this.ngModelCtrl.$invalid && (this.ngModelCtrl.$touched || this.ngModelCtrl.$submitted));
668669
};
669670

670671
/**
@@ -989,12 +990,16 @@
989990
* @param {Date=} value Value that was set to the model.
990991
*/
991992
DatePickerCtrl.prototype.onExternalChange = function(value) {
993+
var self = this;
992994
var timezone = this.$mdUtil.getModelOption(this.ngModelCtrl, 'timezone');
993995

994996
this.date = value;
995997
this.inputElement.value = this.locale.formatDate(value, timezone);
996998
this.mdInputContainer && this.mdInputContainer.setHasValue(!!value);
997999
this.resizeInputElement();
998-
this.updateErrorState();
1000+
// This is often called from the $formatters section of the $validators pipeline.
1001+
// In that case, we need to delay to let $render and $validate run, so that the checks for
1002+
// error state are accurate.
1003+
this.$mdUtil.nextTick(function() {self.updateErrorState();}, false, self.$scope);
9991004
};
10001005
})();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ describe('md-datepicker', function() {
678678
});
679679

680680
scope.$emit('md-calendar-change', new Date());
681+
$timeout.flush();
681682
expect(controller.inputContainer).not.toHaveClass('md-datepicker-invalid');
682683
});
683684
});

0 commit comments

Comments
 (0)