Skip to content

Commit e28002b

Browse files
committed
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 angular#6875
1 parent 0619e6f commit e28002b

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/ng/directive/input.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1229,11 +1229,11 @@ function checkboxInputType(scope, element, attr, ctrl) {
12291229
return value !== trueValue;
12301230
};
12311231

1232-
ctrl.$formatters.push(function(value) {
1232+
ctrl.$formatters.unshift(function(value) {
12331233
return value === trueValue;
12341234
});
12351235

1236-
ctrl.$parsers.push(function(value) {
1236+
ctrl.$parsers.unshift(function(value) {
12371237
return value ? trueValue : falseValue;
12381238
});
12391239
}

test/ng/directive/inputSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,33 @@ describe('input', function() {
18221822
});
18231823

18241824

1825+
it('should allow custom enumeration and ng-required', function() {
1826+
compileInput('<input type="checkbox" ng-model="name" ng-true-value="y" ' +
1827+
'ng-false-value="n" ng-required="true">');
1828+
1829+
scope.$apply(function() {
1830+
scope.name = 'y';
1831+
});
1832+
expect(inputElm[0].checked).toBe(true);
1833+
1834+
scope.$apply(function() {
1835+
scope.name = 'n';
1836+
});
1837+
expect(inputElm[0].checked).toBe(false);
1838+
1839+
scope.$apply(function() {
1840+
scope.name = 'something else';
1841+
});
1842+
expect(inputElm[0].checked).toBe(false);
1843+
1844+
browserTrigger(inputElm, 'click');
1845+
expect(scope.name).toEqual('y');
1846+
1847+
browserTrigger(inputElm, 'click');
1848+
expect(scope.name).toBeUndefined();
1849+
});
1850+
1851+
18251852
it('should be required if false', function() {
18261853
compileInput('<input type="checkbox" ng:model="value" required />');
18271854

0 commit comments

Comments
 (0)