From 65319d1a9ea0c2c0734a6d90c15f6167f5c3e749 Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Wed, 28 Jan 2015 22:38:43 +0100 Subject: [PATCH] fix(validators): check if viewValue is empty, not modelValue --- src/ng/directive/validators.js | 2 +- test/ng/directive/validatorsSpec.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/validators.js b/src/ng/directive/validators.js index bfa20401ce4f..ef7650f10ceb 100644 --- a/src/ng/directive/validators.js +++ b/src/ng/directive/validators.js @@ -65,7 +65,7 @@ var maxlengthDirective = function() { ctrl.$validate(); }); ctrl.$validators.maxlength = function(modelValue, viewValue) { - return (maxlength < 0) || ctrl.$isEmpty(modelValue) || (viewValue.length <= maxlength); + return (maxlength < 0) || ctrl.$isEmpty(viewValue) || (viewValue.length <= maxlength); }; } }; diff --git a/test/ng/directive/validatorsSpec.js b/test/ng/directive/validatorsSpec.js index f22d3c8f130d..3eb352c8b859 100644 --- a/test/ng/directive/validatorsSpec.js +++ b/test/ng/directive/validatorsSpec.js @@ -410,6 +410,20 @@ describe('validators', function() { expect($rootScope.value).toBe(12345); expect($rootScope.form.input.$error.maxlength).toBeUndefined(); }); + + it('should validate emptiness against the viewValue', function() { + var inputElm = helper.compileInput(''); + + var ctrl = inputElm.controller('ngModel'); + spyOn(ctrl, '$isEmpty').andCallThrough(); + + ctrl.$parsers.push(function(value) { + return value + '678'; + }); + + helper.changeInputValueTo('12345'); + expect(ctrl.$isEmpty).toHaveBeenCalledWith('12345'); + }); });