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

Commit 3e51b84

Browse files
committed
fix(input): always pass in the model value to ctrl.$isEmpty
Fixes #5164 Closes #9017
1 parent a0d6e64 commit 3e51b84

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/ng/directive/input.js

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

10021002
ctrl.$render = function() {
1003-
element.val(ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue);
1003+
element.val(ctrl.$isEmpty(ctrl.$modelValue) ? '' : ctrl.$viewValue);
10041004
};
10051005
}
10061006

@@ -1192,8 +1192,7 @@ function urlInputType(scope, element, attr, ctrl, $sniffer, $browser) {
11921192
stringBasedInputType(ctrl);
11931193

11941194
ctrl.$$parserName = 'url';
1195-
ctrl.$validators.url = function(modelValue, viewValue) {
1196-
var value = modelValue || viewValue;
1195+
ctrl.$validators.url = function(value) {
11971196
return ctrl.$isEmpty(value) || URL_REGEXP.test(value);
11981197
};
11991198
}
@@ -1205,8 +1204,7 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12051204
stringBasedInputType(ctrl);
12061205

12071206
ctrl.$$parserName = 'email';
1208-
ctrl.$validators.email = function(modelValue, viewValue) {
1209-
var value = modelValue || viewValue;
1207+
ctrl.$validators.email = function(value) {
12101208
return ctrl.$isEmpty(value) || EMAIL_REGEXP.test(value);
12111209
};
12121210
}
@@ -1260,7 +1258,7 @@ function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $filt
12601258
element[0].checked = ctrl.$viewValue;
12611259
};
12621260

1263-
// Override the standard `$isEmpty` because a value of `false` means empty in a checkbox.
1261+
// Override the standard `$isEmpty` because an empty checkbox is never equal to the trueValue
12641262
ctrl.$isEmpty = function(value) {
12651263
return value !== trueValue;
12661264
};
@@ -1719,7 +1717,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
17191717
* default. The `checkboxInputType` directive does this because in its case a value of `false`
17201718
* implies empty.
17211719
*
1722-
* @param {*} value Reference to check.
1720+
* @param {*} value Model value to check.
17231721
* @returns {boolean} True if `value` is empty.
17241722
*/
17251723
this.$isEmpty = function(value) {
@@ -2471,8 +2469,8 @@ var requiredDirective = function() {
24712469
if (!ctrl) return;
24722470
attr.required = true; // force truthy in case we are on non input element
24732471

2474-
ctrl.$validators.required = function(modelValue, viewValue) {
2475-
return !attr.required || !ctrl.$isEmpty(viewValue);
2472+
ctrl.$validators.required = function(value) {
2473+
return !attr.required || !ctrl.$isEmpty(value);
24762474
};
24772475

24782476
attr.$observe('required', function() {
@@ -2527,7 +2525,7 @@ var maxlengthDirective = function() {
25272525
ctrl.$validate();
25282526
});
25292527
ctrl.$validators.maxlength = function(modelValue, viewValue) {
2530-
return ctrl.$isEmpty(viewValue) || viewValue.length <= maxlength;
2528+
return ctrl.$isEmpty(modelValue) || viewValue.length <= maxlength;
25312529
};
25322530
}
25332531
};
@@ -2546,7 +2544,7 @@ var minlengthDirective = function() {
25462544
ctrl.$validate();
25472545
});
25482546
ctrl.$validators.minlength = function(modelValue, viewValue) {
2549-
return ctrl.$isEmpty(viewValue) || viewValue.length >= minlength;
2547+
return ctrl.$isEmpty(modelValue) || viewValue.length >= minlength;
25502548
};
25512549
}
25522550
};

test/ng/directive/inputSpec.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -3859,7 +3859,7 @@ describe('input', function() {
38593859

38603860

38613861
it('should be required if false', function() {
3862-
compileInput('<input type="checkbox" ng:model="value" required />');
3862+
compileInput('<input type="checkbox" ng-model="value" required />');
38633863

38643864
browserTrigger(inputElm, 'click');
38653865
expect(inputElm[0].checked).toBe(true);
@@ -3869,6 +3869,16 @@ describe('input', function() {
38693869
expect(inputElm[0].checked).toBe(false);
38703870
expect(inputElm).toBeInvalid();
38713871
});
3872+
3873+
it('should set the ngTrueValue when required directive is present', function() {
3874+
compileInput('<input type="checkbox" ng-model="value" required ng-true-value="\'yes\'" />');
3875+
3876+
expect(inputElm).toBeInvalid();
3877+
3878+
browserTrigger(inputElm, 'click');
3879+
expect(inputElm[0].checked).toBe(true);
3880+
expect(inputElm).toBeValid();
3881+
});
38723882
});
38733883

38743884

0 commit comments

Comments
 (0)