Skip to content

Commit 006b078

Browse files
fix(input[date]): do not use $isEmpty to check the model validity
1 parent 3d30cb9 commit 006b078

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/ng/directive/input.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1145,10 +1145,10 @@ function createDateInputType(type, regexp, parseDate, format) {
11451145
});
11461146

11471147
ctrl.$formatters.push(function(value) {
1148-
if (!ctrl.$isEmpty(value)) {
1149-
if (!isDate(value)) {
1150-
throw $ngModelMinErr('datefmt', 'Expected `{0}` to be a date', value);
1151-
}
1148+
if (value && !isDate(value)) {
1149+
throw $ngModelMinErr('datefmt', 'Expected `{0}` to be a date', value);
1150+
}
1151+
if (isValidDate(value)) {
11521152
previousDate = value;
11531153
if (previousDate && timezone === 'UTC') {
11541154
var timezoneOffset = 60000 * previousDate.getTimezoneOffset();
@@ -1157,14 +1157,14 @@ function createDateInputType(type, regexp, parseDate, format) {
11571157
return $filter('date')(value, format, timezone);
11581158
} else {
11591159
previousDate = null;
1160+
return '';
11601161
}
1161-
return '';
11621162
});
11631163

11641164
if (isDefined(attr.min) || attr.ngMin) {
11651165
var minVal;
11661166
ctrl.$validators.min = function(value) {
1167-
return ctrl.$isEmpty(value) || isUndefined(minVal) || parseDate(value) >= minVal;
1167+
return !isValidDate(value) || isUndefined(minVal) || parseDate(value) >= minVal;
11681168
};
11691169
attr.$observe('min', function(val) {
11701170
minVal = parseObservedDateValue(val);
@@ -1175,18 +1175,18 @@ function createDateInputType(type, regexp, parseDate, format) {
11751175
if (isDefined(attr.max) || attr.ngMax) {
11761176
var maxVal;
11771177
ctrl.$validators.max = function(value) {
1178-
return ctrl.$isEmpty(value) || isUndefined(maxVal) || parseDate(value) <= maxVal;
1178+
return !isValidDate(value) || isUndefined(maxVal) || parseDate(value) <= maxVal;
11791179
};
11801180
attr.$observe('max', function(val) {
11811181
maxVal = parseObservedDateValue(val);
11821182
ctrl.$validate();
11831183
});
11841184
}
1185-
// Override the standard $isEmpty to detect invalid dates as well
1186-
ctrl.$isEmpty = function(value) {
1185+
1186+
function isValidDate(value) {
11871187
// Invalid Date: getTime() returns NaN
1188-
return !value || (value.getTime && value.getTime() !== value.getTime());
1189-
};
1188+
return value && !(value.getTime && value.getTime() !== value.getTime());
1189+
}
11901190

11911191
function parseObservedDateValue(val) {
11921192
return isDefined(val) ? (isDate(val) ? val : parseDate(val)) : undefined;

0 commit comments

Comments
 (0)