From 8f17b9f11bd359fe0066133af8b93914611150ea Mon Sep 17 00:00:00 2001 From: Michael Benford Date: Thu, 19 Mar 2015 23:35:39 -0300 Subject: [PATCH] feat(tagsInput): Add ng-required support Add support for the ng-required directive so the required validation key is set when there are no tags in the model bound to the tagsInput directive. Closes #157 --- src/tags-input.js | 4 ++++ test/tags-input.spec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/tags-input.js b/src/tags-input.js index 33e3beff..f06c4ec7 100644 --- a/src/tags-input.js +++ b/src/tags-input.js @@ -229,6 +229,10 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, tagsInpu ngModelCtrl.$setValidity('leftoverText', scope.hasFocus || options.allowLeftoverText ? true : !scope.newTag.text); }; + ngModelCtrl.$isEmpty = function(value) { + return !value || !value.length; + }; + scope.newTag = { text: '', invalid: null, diff --git a/test/tags-input.spec.js b/test/tags-input.spec.js index a7440c87..fbc89fa5 100644 --- a/test/tags-input.spec.js +++ b/test/tags-input.spec.js @@ -1676,6 +1676,36 @@ describe('tags-input directive', function() { }); }); + describe('ng-required support', function() { + it('sets the required validation key when there is no tags', function() { + // Arrange/Act + compileWithForm('name="tags"', 'ng-required="true"'); + + // Assert + expect($scope.form.tags.$invalid).toBe(true); + expect($scope.form.tags.$error.required).toBe(true); + }); + + it('doesn\'t set the required validation key when there is any tags', function() { + // Arrange/Act + $scope.tags = ['Tag']; + compileWithForm('name="tags"', 'ng-required="true"'); + + // Assert + expect($scope.form.tags.$invalid).toBe(false); + expect($scope.form.tags.$error.required).toBe(false); + }); + + it('doesn\'t set the required validation key when ng-required is false', function() { + // Arrange/Act + compileWithForm('name="tags"', 'ng-required="false"'); + + // Assert + expect($scope.form.tags.$invalid).toBe(false); + expect($scope.form.tags.$error.required).toBe(false); + }); + }); + describe('autocomplete registration', function() { var autocompleteObj;