link: function ($scope, element, attrs, ngModel) { if (!ngModel) return; originalValue = ngModel.$modelValue; $scope.$watch('valueToCompare', function (valueToCompare) { // watches for changes from valueToCompare binding ngModel.$validate(); }); ngModel.$validators.codeUnique = function (modelValue, viewValue) { let status = true; if (_.isEmpty(viewValue.trim()) || _.isEmpty($scope.valueToCompare.trim())) { status = true; } else { if (viewValue.trim().toUpperCase() === $scope.valueToCompare.trim().toUpperCase()) { status = false; } } return status; };There is still yet a problem but I think I'll leave this one. Say, I entered the same code in UPC as in the barcode and then deleted the barcode value. I still get the error now coming from asyncValidators saying that the code already exists in the barcode for this item. I think making it smarter would be tough.
if (_.isObject($scope.objectToCompare)) { ngModel.$validators.codeUnique = function (modelValue, viewValue) { let status = true; if (_.isEmpty(viewValue.trim()) || _.isEmpty($scope.objectToCompare.$modelValue.trim())) { status = true; } else { if (viewValue.trim().toUpperCase() === $scope.objectToCompare.$modelValue.trim().toUpperCase()) { status = false; } } return status; }; }I have two controls on the form - UPC and Barcode and I validate each against the other. So, suppose I entered the same value in UPC as in my barcode. I got the error message on UPC.