From e28002b4ed2d8e54cc654b561c907a5f61a4140b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Galfas=C3=B3?= Date: Thu, 27 Mar 2014 12:40:55 -0300 Subject: [PATCH] fix(input): Allow ng-true-value and ng-required on a checkbox Put the input[checkbox] formatter and parser first in the $formatters and $parsers queue so they are the first being called when formatting and the last when parsing Closes #6875 --- src/ng/directive/input.js | 4 ++-- test/ng/directive/inputSpec.js | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 2f78db049edd..5bac0c456f66 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1229,11 +1229,11 @@ function checkboxInputType(scope, element, attr, ctrl) { return value !== trueValue; }; - ctrl.$formatters.push(function(value) { + ctrl.$formatters.unshift(function(value) { return value === trueValue; }); - ctrl.$parsers.push(function(value) { + ctrl.$parsers.unshift(function(value) { return value ? trueValue : falseValue; }); } diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index eba3028e7bce..baeb2ff583d3 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -1822,6 +1822,33 @@ describe('input', function() { }); + it('should allow custom enumeration and ng-required', function() { + compileInput(''); + + scope.$apply(function() { + scope.name = 'y'; + }); + expect(inputElm[0].checked).toBe(true); + + scope.$apply(function() { + scope.name = 'n'; + }); + expect(inputElm[0].checked).toBe(false); + + scope.$apply(function() { + scope.name = 'something else'; + }); + expect(inputElm[0].checked).toBe(false); + + browserTrigger(inputElm, 'click'); + expect(scope.name).toEqual('y'); + + browserTrigger(inputElm, 'click'); + expect(scope.name).toBeUndefined(); + }); + + it('should be required if false', function() { compileInput('');