Skip to content

Commit 3d30cb9

Browse files
revert: fix(input): always pass in the model value to ctrl.$isEmpty
This commit tried to create consistency by ensuring that `$isEmpty` is not called on both model and view values but it chose to only use `$modelValue`, which is not actually correct. `$isEmpty` is designed to compute whether the `$viewValue` is empty. In practice this is the only part of the parse/format system that the directive has control over. We can't rely on the `$modelValue` being in any particular format since other directives can add in their own formatters and parsers to completely change this. (reverted from commit 3e51b84)
1 parent 5c43b94 commit 3d30cb9

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/ng/directive/input.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10351035
element.on('change', listener);
10361036

10371037
ctrl.$render = function() {
1038-
element.val(ctrl.$isEmpty(ctrl.$modelValue) ? '' : ctrl.$viewValue);
1038+
element.val(ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue);
10391039
};
10401040
}
10411041

@@ -1270,7 +1270,8 @@ function urlInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12701270
stringBasedInputType(ctrl);
12711271

12721272
ctrl.$$parserName = 'url';
1273-
ctrl.$validators.url = function(value) {
1273+
ctrl.$validators.url = function(modelValue, viewValue) {
1274+
var value = modelValue || viewValue;
12741275
return ctrl.$isEmpty(value) || URL_REGEXP.test(value);
12751276
};
12761277
}
@@ -1282,7 +1283,8 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12821283
stringBasedInputType(ctrl);
12831284

12841285
ctrl.$$parserName = 'email';
1285-
ctrl.$validators.email = function(value) {
1286+
ctrl.$validators.email = function(modelValue, viewValue) {
1287+
var value = modelValue || viewValue;
12861288
return ctrl.$isEmpty(value) || EMAIL_REGEXP.test(value);
12871289
};
12881290
}
@@ -1336,7 +1338,7 @@ function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $filt
13361338
element[0].checked = ctrl.$viewValue;
13371339
};
13381340

1339-
// Override the standard `$isEmpty` because an empty checkbox is never equal to the trueValue
1341+
// Override the standard `$isEmpty` because a value of `false` means empty in a checkbox.
13401342
ctrl.$isEmpty = function(value) {
13411343
return value !== trueValue;
13421344
};
@@ -1812,7 +1814,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
18121814
* default. The `checkboxInputType` directive does this because in its case a value of `false`
18131815
* implies empty.
18141816
*
1815-
* @param {*} value Model value to check.
1817+
* @param {*} value Reference to check.
18161818
* @returns {boolean} True if `value` is empty.
18171819
*/
18181820
this.$isEmpty = function(value) {
@@ -2640,8 +2642,8 @@ var requiredDirective = function() {
26402642
if (!ctrl) return;
26412643
attr.required = true; // force truthy in case we are on non input element
26422644

2643-
ctrl.$validators.required = function(value) {
2644-
return !attr.required || !ctrl.$isEmpty(value);
2645+
ctrl.$validators.required = function(modelValue, viewValue) {
2646+
return !attr.required || !ctrl.$isEmpty(viewValue);
26452647
};
26462648

26472649
attr.$observe('required', function() {
@@ -2696,7 +2698,7 @@ var maxlengthDirective = function() {
26962698
ctrl.$validate();
26972699
});
26982700
ctrl.$validators.maxlength = function(modelValue, viewValue) {
2699-
return ctrl.$isEmpty(modelValue) || viewValue.length <= maxlength;
2701+
return ctrl.$isEmpty(viewValue) || viewValue.length <= maxlength;
27002702
};
27012703
}
27022704
};
@@ -2715,7 +2717,7 @@ var minlengthDirective = function() {
27152717
ctrl.$validate();
27162718
});
27172719
ctrl.$validators.minlength = function(modelValue, viewValue) {
2718-
return ctrl.$isEmpty(modelValue) || viewValue.length >= minlength;
2720+
return ctrl.$isEmpty(viewValue) || viewValue.length >= minlength;
27192721
};
27202722
}
27212723
};

0 commit comments

Comments
 (0)