I'm trying to validate a form where there are no duplication of values. I have a site contain a list of Ip adress. In my from the is possible to add many adresses but they should be unique.
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>
I also tried with ng-change:"verifyDuplicate()" and here is the 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));
}
}
and this throw an error: Cannot read property 'ips' of undefined.
I also tried this example but it doesnt work.
Any suggestions please ?
site.ipsas source (site.ipssupposed to be array) and inverifyDuplicateyou usesites[0](sitessupposed to be array). This should match. Try$scope.site.ips[0]in js$duplicateyou must use a directive, otherwise the form will keeps valid.