diff options
| -rw-r--r-- | src/directive/input.js | 4 | ||||
| -rw-r--r-- | test/directive/inputSpec.js | 33 |
2 files changed, 30 insertions, 7 deletions
diff --git a/src/directive/input.js b/src/directive/input.js index 1a3acd01..7b95baaf 100644 --- a/src/directive/input.js +++ b/src/directive/input.js @@ -847,7 +847,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel', value = formatters[idx](value); } - if (isDefined(value) && ctrl.viewValue !== value) { + if (ctrl.viewValue !== value) { ctrl.viewValue = value; ctrl.render(); } @@ -1042,7 +1042,7 @@ var requiredDirective = [function() { if (!ctrl) return; var validator = function(value) { - if (attr.required && isEmpty(value)) { + if (attr.required && (isEmpty(value) || value === false)) { ctrl.setValidity('REQUIRED', false); return null; } else { diff --git a/test/directive/inputSpec.js b/test/directive/inputSpec.js index 6298fc64..653dfdc6 100644 --- a/test/directive/inputSpec.js +++ b/test/directive/inputSpec.js @@ -183,22 +183,32 @@ describe('NgModelController', function() { }); - it('should $render only if value changed and is valid', function() { + it('should $render only if value changed', function() { spyOn(ctrl, 'render'); scope.$apply(function() { - scope.value= 3; + scope.value = 3; }); expect(ctrl.render).toHaveBeenCalledOnce(); ctrl.render.reset(); - // invalid - ctrl.formatters.push(function() {return undefined;}); + ctrl.formatters.push(function() {return 3;}); scope.$apply(function() { - scope.value= 5; + scope.value = 5; }); expect(ctrl.render).not.toHaveBeenCalled(); }); + + + it('should clear the view even if invalid', function() { + spyOn(ctrl, 'render'); + + ctrl.formatters.push(function() {return undefined;}); + scope.$apply(function() { + scope.value = 5; + }); + expect(ctrl.render).toHaveBeenCalledOnce(); + }); }); }); @@ -729,6 +739,19 @@ describe('input', function() { browserTrigger(inputElm, 'click'); expect(scope.name).toEqual('n'); }); + + + it('should be required if false', function() { + compileInput('<input type="checkbox" ng:model="value" required />'); + + browserTrigger(inputElm, 'click'); + expect(inputElm[0].checked).toBe(true); + expect(inputElm).toBeValid(); + + browserTrigger(inputElm, 'click'); + expect(inputElm[0].checked).toBe(false); + expect(inputElm).toBeInvalid(); + }); }); |
