title: Duplicate input validation with AngularJS
body: <p>I'm using a custom directive in AngularJS to try to validate a form which is created using ng-repeat. I want to validate one of the input fields to ensure the user can't enter the same value more than once when there are multiple rows. Here's what I've got so far...</p>
  
  <pre><code>angular.module('vehiclesApp').directive('drDuplicate', function() {
  
    return {
      restrict: 'A',
      //scope: true,
      require: 'ngModel',
      link: function(scope, elem, attrs, ctrl) {
        ctrl.$validators.duplicate = function(modelValue) {
  
          //console.log(modelValue);
          //console.log(scope.rows);
          //return modelValue % 2 === 1;
          //return modelValue.length &gt; 1;
  
          //if(scope.rows.length &gt; 1) {
          //  var arr = [];
          //  for(j=0, i=1; i&lt;=scope.rows.length; i++, j++) {
          //    //if(scope.rows[j].vrm != '') {
          //      arr.push(scope.rows[j].vrm);
          //    //}
          //  }
          //}
          //console.log(arr);
        };
      }
    };
  });
  </code></pre>
  
  <p>I've tried to create a loop in order to push the values into an array, as I think it will be easier to sort through. Can anyone point me in the right direction to build a separate function which will allow me to pass errors back to the UI (which is using ng-messages with ng-if="submitted")?</p>