Search code examples
angularjsng-tags-input

Required on a ngTagsInput doesn't work


I am using the ngTagsInput library in my app. Setting the required keyword is not affecting form validation. Why may this be the case?

<tags-input name="tags" min-length="2" add-on-paste="true" class="bootstrap" ng-model="name" id="name" placeholder="Name" required></tags-input>

Solution

  • The required directive probably only works on regular text inputs. Instead, you can use the minTags attribute of ngTagsInput:

    <form name="myForm">
        <tags-input name="tags" min-tags="1"></tags-input>
        <p ng-show="myForm.tags.$error.minTags">Tag required</p>
    </form>
    

    Then you can use the minTags validation error key instead of required error key to know if the user has entered at least one tag.