2016-07-16 83 views
1

我想驗證一個沒有重複值的表單。 我有一個網站包含IP地址列表。 在我可以添加許多地址,但它們應該是唯一的。角度檢查複製輸入

HTML:

<div class="col-sm-6"> 
    <ul style="list-style-type: none;-webkit-padding-start: 0px;margin-bottom:0px"> 
    <li ng-repeat="ip in site.ips track by $index | orderBy: '+ip.ip_adress'" style="margin-bottom:0px"> 
     <ng-form name="ipForm">      
     <div class="input-group" style="margin-top:5px" ng-class="{ 'has-error' : !ipForm.ip_adress.$error.duplicate && ipForm.ip_adress.$invalid && !ipForm.ip_adress.$pristine }"> 
      <input type="text" class="form-control input-sm" name="ip_adress" 
      ng-pattern='/^([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})$/' 
      style="display: inline;" 
      ng-model="ip.ip_adress" 
      placeholder="{{translate('config.sites.msg.site.ip.adress')}}"      
      required /> 
      <div class="input-group-addon"> 
       <i class="glyphicon glyphicon-remove-circle" ng-click="removeIp(site, ip, $index)"></i> 
      </div> 
     </div> 
     </ng-form> 

    </li> 
    </ul> 
</div> 

我也試圖與ng-change:"verifyDuplicate()"這裏是JS

$scope.verifyDuplicate = function() { 
       var sorted, i; 
       sorted = $scope.sites[0].ips.sort(function (a, b) { 
        if (a.ip_adress > b.ip_adress) return 1; 
        if (a.ip_adress < b.ip_adress) return -1; 
        return 0; 
       }); 
       for(i = 0; i < $scope.sites.ips.length; i++) { 
        sorted[i].isDuplicate = ((sorted[i-1] && sorted[i-1].ip_adress == sorted[i].ip_adress) || (sorted[i+1] && sorted[i+1].ip_adress == sorted[i].ip_adress)); 
       } 


      } 

,這引發錯誤:Cannot read property 'ips' of undefined。 我也試過這個例子,但它不起作用。

有什麼建議嗎?

+0

你可以顯示$ scope.sites –

+0

的結構,你可以使用'site.ips'作爲源代碼('site.ips'應該是數組),在'verifyDuplicate'中使用'sites [0 ]'('sites'應該是數組)。這應該匹配。在js –

+1

中試試'$ scope.site.ips [0]'除了你的代碼有很多錯誤之外,如果你想設置'$ duplicate'的驗證,你必須**使用指令,否則表格將保持有效。 – developer033

回答

1

必須建立一個指令,如果你想控制你的輸入的validity,那麼你的表單將變成$invalid

這裏有一個簡單的演示,你可以在你的代碼複製:

angular.module('app', ['ngMessages']) 
 
    .controller('mainCtrl', function($scope) { 
 
    $scope.site = {}; 
 
    $scope.site.ips = ['172.16.254.1', '255.255.255.255', '111.111.111.111'] 
 
    }) 
 

 
    .directive('duplicate', function() { 
 
    return { 
 
     restrict: 'A', 
 
     scope: { 
 
     index: '@', 
 
     array: '@' 
 
     }, 
 
     require: 'ngModel', 
 
     link: function(scope, element, attrs, ngModel) { 
 
     if (!ngModel) return; 
 

 
     scope.$watch(function() { 
 
      return ngModel.$modelValue 
 
      }, 
 
      function() { 
 
      validate(); 
 
      }); 
 

 
     var validate = function() { 
 
      scope.array = JSON.parse(scope.array); 
 
      var elem_value = element.val(); 
 
      var obj = scope.array.find(function(value, i) { 
 
      return value == elem_value && scope.index != i; 
 
      }); 
 
      ngModel.$setValidity('duplicate', obj ? false : true); 
 
     }; 
 
     } 
 
    } 
 
    });
<!DOCTYPE html> 
 
<html ng-app="app"> 
 

 
<head> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script> 
 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" /> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-messages/1.5.7/angular-messages.min.js"></script> 
 
</head> 
 

 
<body ng-controller="mainCtrl"> 
 
    <div class="col-sm-6"> 
 
    <ul style="list-style-type: none;-webkit-padding-start: 0px;margin-bottom:0px"> 
 
     <li ng-repeat="ip in site.ips track by $index" style="margin-bottom:0px"> 
 
     <ng-form name="ipForm"> 
 
      <div class="input-group" style="margin-top:5px" ng-class="{ 'has-error' : ipForm.ip_address.$invalid }"> 
 
      <input type="text" name="ip_address" class="form-control input-sm" ng-pattern='/^([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})$/' style="display: inline;" ng-model="site.ips[$index]" placeholder="{{translate('config.sites.msg.site.ip.adress')}}" 
 
      required duplicate index="{{$index}}" array="{{site.ips}}" /> 
 
      <div class="input-group-addon"> 
 
       <i class="glyphicon glyphicon-remove-circle" ng-click="removeIp(site, ip, $index)"></i> 
 
      </div> 
 
      <div class="help-block" ng-messages="ipForm.ip_address.$error" ng-if="ipForm.ip_address.$touched"> 
 
       <p ng-message="required">This field is required</p> 
 
       <p ng-message="minlength">This field is too short</p> 
 
       <p ng-message="maxlength">This field is too long</p> 
 
       <p ng-message="pattern">This needs to be a valid ip</p> 
 
       <p ng-message="duplicate">Duplicate!</p> 
 
      </div> 
 
      </div> 
 
     </ng-form> 
 
     </li> 
 
    </ul> 
 
    </div> 
 
</body> 
 

 
</html>

注:這只是給你指出正確的方向,也許它不是最有效的方法來做到這一點。

無論如何,我希望它有幫助。