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

Fixes #5164
Closes #9017
This commit is contained in:
Tobias Bosch
2014-09-10 11:00:50 -07:00
parent a0d6e64889
commit 3e51b84bc1
2 changed files with 20 additions and 12 deletions

View File

@@ -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);
};
}
@@ -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);
};
}
@@ -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);
};
}
@@ -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 never equal to the trueValue
ctrl.$isEmpty = function(value) {
return value !== trueValue;
};
@@ -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.
* @returns {boolean} True if `value` is empty.
*/
this.$isEmpty = function(value) {
@@ -2471,8 +2469,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() {
@@ -2527,7 +2525,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;
};
}
};
@@ -2546,7 +2544,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;
};
}
};

View File

@@ -3859,7 +3859,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);
@@ -3869,6 +3869,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();
});
});