From fb9b66731b782d984ed1c333df0de7050b36b78d Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Wed, 10 Sep 2014 11:00:50 -0700 Subject: [PATCH] fix(input): always pass in the model value to `ctrl.$isEmpty` Fixes #5164 --- src/ng/directive/input.js | 20 +++++++++----------- test/ng/directive/inputSpec.js | 12 +++++++++++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 4c55adc8cf5b..dd6e7aed8555 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -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 always not 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) { @@ -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() { @@ -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; }; } }; @@ -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; }; } }; diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 169c8d6da9b7..5d923662a783 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -3805,7 +3805,7 @@ describe('input', function() { it('should be required if false', function() { - compileInput(''); + compileInput(''); browserTrigger(inputElm, 'click'); expect(inputElm[0].checked).toBe(true); @@ -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(''); + + expect(inputElm).toBeInvalid(); + + browserTrigger(inputElm, 'click'); + expect(inputElm[0].checked).toBe(true); + expect(inputElm).toBeValid(); + }); });