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

fix(input): always pass in the model value to ctrl.$isEmpty #9017

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
element.on('change', listener);

ctrl.$render = function() {
element.val(ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue);
element.val(ctrl.$isEmpty(ctrl.$modelValue) ? '' : ctrl.$viewValue);
};
}

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

ctrl.$$parserName = 'url';
ctrl.$validators.url = function(modelValue, viewValue) {
var value = modelValue || viewValue;
ctrl.$validators.url = function(value) {
return ctrl.$isEmpty(value) || URL_REGEXP.test(value);
};
}
Expand All @@ -1205,8 +1204,7 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
stringBasedInputType(ctrl);

ctrl.$$parserName = 'email';
ctrl.$validators.email = function(modelValue, viewValue) {
var value = modelValue || viewValue;
ctrl.$validators.email = function(value) {
return ctrl.$isEmpty(value) || EMAIL_REGEXP.test(value);
};
}
Expand Down Expand Up @@ -1260,7 +1258,7 @@ function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $filt
element[0].checked = ctrl.$viewValue;
};

// Override the standard `$isEmpty` because a value of `false` means empty in a checkbox.
// Override the standard `$isEmpty` because an empty checkbox is always not equal to the trueValue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to reword as "an empty checkbox is never equal to the trueValue"?

ctrl.$isEmpty = function(value) {
return value !== trueValue;
};
Expand Down Expand Up @@ -1719,7 +1717,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
* default. The `checkboxInputType` directive does this because in its case a value of `false`
* implies empty.
*
* @param {*} value Reference to check.
* @param {*} value model value to check.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be capitalized: s/model/Model

* @returns {boolean} True if `value` is empty.
*/
this.$isEmpty = function(value) {
Expand Down Expand Up @@ -2463,8 +2461,8 @@ var requiredDirective = function() {
if (!ctrl) return;
attr.required = true; // force truthy in case we are on non input element

ctrl.$validators.required = function(modelValue, viewValue) {
return !attr.required || !ctrl.$isEmpty(viewValue);
ctrl.$validators.required = function(value) {
return !attr.required || !ctrl.$isEmpty(value);
};

attr.$observe('required', function() {
Expand Down Expand Up @@ -2519,7 +2517,7 @@ var maxlengthDirective = function() {
ctrl.$validate();
});
ctrl.$validators.maxlength = function(modelValue, viewValue) {
return ctrl.$isEmpty(viewValue) || viewValue.length <= maxlength;
return ctrl.$isEmpty(modelValue) || viewValue.length <= maxlength;
};
}
};
Expand All @@ -2538,7 +2536,7 @@ var minlengthDirective = function() {
ctrl.$validate();
});
ctrl.$validators.minlength = function(modelValue, viewValue) {
return ctrl.$isEmpty(viewValue) || viewValue.length >= minlength;
return ctrl.$isEmpty(modelValue) || viewValue.length >= minlength;
};
}
};
Expand Down
12 changes: 11 additions & 1 deletion test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3805,7 +3805,7 @@ describe('input', function() {


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

browserTrigger(inputElm, 'click');
expect(inputElm[0].checked).toBe(true);
Expand All @@ -3815,6 +3815,16 @@ describe('input', function() {
expect(inputElm[0].checked).toBe(false);
expect(inputElm).toBeInvalid();
});

it('should set the ngTrueValue when required directive is present', function() {
compileInput('<input type="checkbox" ng-model="value" required ng-true-value="\'yes\'" />');

expect(inputElm).toBeInvalid();

browserTrigger(inputElm, 'click');
expect(inputElm[0].checked).toBe(true);
expect(inputElm).toBeValid();
});
});


Expand Down