title: Angularjs - How to check for unique inputs and if there is a duplicate mark both as invalid
body: <p>I have a situation where a user needs to enter input into text areas created by ng-repeat.
  If the user enters a value that has already been entered both the new and existing values should be validated as false. If one of the values (existing or new) gets changed, the validation should be updated correspondingly. </p>
  
  <p>I have tried quite a variety of options, currently this is what comes close, but is still not 100%.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;body ng-app="ap" ng-controller="con"&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;name&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr ng-repeat="person in persons"&gt;
          &lt;td&gt;
              &lt;ng-form name="personForm"&gt;
              &lt;div ng-class="{ 'has-error' : 
                  personForm.personName.$invalid }"&gt;
                  &lt;input type='text'
                  name="personName"
                  ng-class="empty"
                  ng-model="person.name"
                  ng-change="verifyDuplicate(this, person)"/&gt;
                  &lt;/div&gt;
               &lt;/ng-form&gt; 
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt; 
  </code></pre>
  
  <p></p>
  
  <p>JavaScript:</p>
  
  <pre><code>var app = angular.module("ap",[]);
  
      app.controller("con",function($scope){
  
      $scope.persons = [
          {name: 'a'},
          {name: 'b'},
          {name: 'c'}
      ];
  
      $scope.empty = "normal";
  
      $scope.verifyDuplicate = function(domScope, object){
          for(var i = 0; i &lt; $scope.persons.length; i++) {
              if($scope.persons[i].name === object.name &amp;&amp; $scope.persons[i] !== object) {
                  domScope.personForm.personName.$setValidity('duplicate',false);
              }
              else {
                  domScope.personForm.personName.$setValidity('duplicate',true);
              }
          }
      };
  });
  </code></pre>
  
  <p>Any help on this would be appreciated.</p>
  
  <p>Here is a fiddle <a href="http://jsfiddle.net/staalburger/z5ja2wy0/" rel="noreferrer">Fiddle of code</a></p>