From b56b21a898b3c77589a48a290271f9dc181dafe8 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Wed, 25 Sep 2013 12:49:55 +0100 Subject: fix(input): `false` is no longer an empty value by default `checkboxInputType` and `ngList` directives need to have special logic for whether they are empty or not. Previously this had been hard coded into their own directives or the `ngRequired` directive. This made it difficult to handle these special cases. This change factors out the question of whether an input is empty into a method `$isEmpty` on the `ngModelController`. The `ngRequired` directive now uses this method when testing for validity and directives, such as `checkbox` or `ngList` can override it to apply logic specific to their needs. Closes #3490, #3658, #2594 --- test/ng/directive/inputSpec.js | 33 +++++++++++++++++++++++++++++++-- test/ng/directive/selectSpec.js | 25 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index facc2b80..c94eb9b8 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -998,6 +998,16 @@ describe('input', function() { expect(scope.list).toEqual([]); }); + it('should be invalid if required and empty', function() { + compileInput(''); + changeInputValueTo(''); + expect(scope.list).toBeUndefined(); + expect(inputElm).toBeInvalid(); + changeInputValueTo('a,b'); + expect(scope.list).toEqual(['a','b']); + expect(inputElm).toBeValid(); + }); + it('should allow custom separator', function() { compileInput(''); @@ -1090,10 +1100,29 @@ describe('input', function() { it('should set $invalid when model undefined', function() { - compileInput(''); + compileInput(''); scope.$digest(); expect(inputElm).toBeInvalid(); - }) + }); + + + it('should allow `false` as a valid value when the input type is not "checkbox"', function() { + compileInput('' + + ''); + + scope.$apply(); + expect(inputElm).toBeInvalid(); + + scope.$apply(function() { + scope.answer = true; + }); + expect(inputElm).toBeValid(); + + scope.$apply(function() { + scope.answer = false; + }); + expect(inputElm).toBeValid(); + }); }); diff --git a/test/ng/directive/selectSpec.js b/test/ng/directive/selectSpec.js index 85acba19..ac0cc70d 100644 --- a/test/ng/directive/selectSpec.js +++ b/test/ng/directive/selectSpec.js @@ -1213,6 +1213,31 @@ describe('select', function() { }); expect(element).toBeValid(); }); + + + it('should allow falsy values as values', function() { + createSelect({ + 'ng-model': 'value', + 'ng-options': 'item.value as item.name for item in values', + 'ng-required': 'required' + }, true); + + scope.$apply(function() { + scope.values = [{name: 'True', value: true}, {name: 'False', value: false}]; + scope.required = false; + }); + + element.val('1'); + browserTrigger(element, 'change'); + expect(element).toBeValid(); + expect(scope.value).toBe(false); + + scope.$apply(function() { + scope.required = true; + }); + expect(element).toBeValid(); + expect(scope.value).toBe(false); + }); }); }); -- cgit v1.2.3