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

Commit 4644c58

Browse files
committedNov 23, 2014
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 addb4bd commit 4644c58

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed
 

‎src/ng/directive/input.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10391039
element.on('change', listener);
10401040

10411041
ctrl.$render = function() {
1042-
element.val(ctrl.$isEmpty(ctrl.$modelValue) ? '' : ctrl.$viewValue);
1042+
element.val(ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue);
10431043
};
10441044
}
10451045

@@ -1274,7 +1274,8 @@ function urlInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12741274
stringBasedInputType(ctrl);
12751275

12761276
ctrl.$$parserName = 'url';
1277-
ctrl.$validators.url = function(value) {
1277+
ctrl.$validators.url = function(modelValue, viewValue) {
1278+
var value = modelValue || viewValue;
12781279
return ctrl.$isEmpty(value) || URL_REGEXP.test(value);
12791280
};
12801281
}
@@ -1286,7 +1287,8 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12861287
stringBasedInputType(ctrl);
12871288

12881289
ctrl.$$parserName = 'email';
1289-
ctrl.$validators.email = function(value) {
1290+
ctrl.$validators.email = function(modelValue, viewValue) {
1291+
var value = modelValue || viewValue;
12901292
return ctrl.$isEmpty(value) || EMAIL_REGEXP.test(value);
12911293
};
12921294
}
@@ -1340,7 +1342,7 @@ function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $filt
13401342
element[0].checked = ctrl.$viewValue;
13411343
};
13421344

1343-
// Override the standard `$isEmpty` because an empty checkbox is never equal to the trueValue
1345+
// Override the standard `$isEmpty` because a value of `false` means empty in a checkbox.
13441346
ctrl.$isEmpty = function(value) {
13451347
return value !== trueValue;
13461348
};
@@ -1819,7 +1821,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
18191821
* default. The `checkboxInputType` directive does this because in its case a value of `false`
18201822
* implies empty.
18211823
*
1822-
* @param {*} value Model value to check.
1824+
* @param {*} value Reference to check.
18231825
* @returns {boolean} True if `value` is empty.
18241826
*/
18251827
this.$isEmpty = function(value) {
@@ -2647,8 +2649,8 @@ var requiredDirective = function() {
26472649
if (!ctrl) return;
26482650
attr.required = true; // force truthy in case we are on non input element
26492651

2650-
ctrl.$validators.required = function(value) {
2651-
return !attr.required || !ctrl.$isEmpty(value);
2652+
ctrl.$validators.required = function(modelValue, viewValue) {
2653+
return !attr.required || !ctrl.$isEmpty(viewValue);
26522654
};
26532655

26542656
attr.$observe('required', function() {
@@ -2723,7 +2725,7 @@ var minlengthDirective = function() {
27232725
ctrl.$validate();
27242726
});
27252727
ctrl.$validators.minlength = function(modelValue, viewValue) {
2726-
return ctrl.$isEmpty(modelValue) || viewValue.length >= minlength;
2728+
return ctrl.$isEmpty(viewValue) || viewValue.length >= minlength;
27272729
};
27282730
}
27292731
};

0 commit comments

Comments
 (0)