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

fix(input): make checkbox ngTrueValue work with required #8979

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 @@ -1253,9 +1253,9 @@ 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 set to `false`
ctrl.$isEmpty = function(value) {
return value !== trueValue;
return value === false;
};

ctrl.$formatters.push(function(value) {
Expand Down
12 changes: 11 additions & 1 deletion test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3623,7 +3623,7 @@ describe('input', function() {


it('should be required if false', function() {
compileInput('<input type="checkbox" ng:model="value" required />');
compileInput('<input type="checkbox" ng-model="value" required />');

browserTrigger(inputElm, 'click');
expect(inputElm[0].checked).toBe(true);
Expand All @@ -3633,6 +3633,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('<input type="checkbox" ng-model="value" required ng-true-value="\'yes\'" />');

expect(inputElm).toBeInvalid();

browserTrigger(inputElm, 'click');
expect(inputElm[0].checked).toBe(true);
expect(inputElm).toBeValid();
});
});


Expand Down