Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(input): Allow ng-true-value and ng-required on a checkbox #6876

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand Down
27 changes: 27 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,33 @@ describe('input', function() {
});


it('should allow custom enumeration and ng-required', function() {
compileInput('<input type="checkbox" ng-model="name" ng-true-value="y" ' +
'ng-false-value="n" ng-required="true">');

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('<input type="checkbox" ng:model="value" required />');

Expand Down