Skip to content

Commit fb9b667

Browse files
committed
fix(input): always pass in the model value to ctrl.$isEmpty
Fixes angular#5164
1 parent 3c538c1 commit fb9b667

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 always not 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) {
@@ -2463,8 +2461,8 @@ var requiredDirective = function() {
24632461
if (!ctrl) return;
24642462
attr.required = true; // force truthy in case we are on non input element
24652463

2466-
ctrl.$validators.required = function(modelValue, viewValue) {
2467-
return !attr.required || !ctrl.$isEmpty(viewValue);
2464+
ctrl.$validators.required = function(value) {
2465+
return !attr.required || !ctrl.$isEmpty(value);
24682466
};
24692467

24702468
attr.$observe('required', function() {
@@ -2519,7 +2517,7 @@ var maxlengthDirective = function() {
25192517
ctrl.$validate();
25202518
});
25212519
ctrl.$validators.maxlength = function(modelValue, viewValue) {
2522-
return ctrl.$isEmpty(viewValue) || viewValue.length <= maxlength;
2520+
return ctrl.$isEmpty(modelValue) || viewValue.length <= maxlength;
25232521
};
25242522
}
25252523
};
@@ -2538,7 +2536,7 @@ var minlengthDirective = function() {
25382536
ctrl.$validate();
25392537
});
25402538
ctrl.$validators.minlength = function(modelValue, viewValue) {
2541-
return ctrl.$isEmpty(viewValue) || viewValue.length >= minlength;
2539+
return ctrl.$isEmpty(modelValue) || viewValue.length >= minlength;
25422540
};
25432541
}
25442542
};

test/ng/directive/inputSpec.js

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

38063806

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

38103810
browserTrigger(inputElm, 'click');
38113811
expect(inputElm[0].checked).toBe(true);
@@ -3815,6 +3815,16 @@ describe('input', function() {
38153815
expect(inputElm[0].checked).toBe(false);
38163816
expect(inputElm).toBeInvalid();
38173817
});
3818+
3819+
it('should set the ngTrueValue when required directive is present', function() {
3820+
compileInput('<input type="checkbox" ng-model="value" required ng-true-value="\'yes\'" />');
3821+
3822+
expect(inputElm).toBeInvalid();
3823+
3824+
browserTrigger(inputElm, 'click');
3825+
expect(inputElm[0].checked).toBe(true);
3826+
expect(inputElm).toBeValid();
3827+
});
38183828
});
38193829

38203830

0 commit comments

Comments
 (0)