I am currently working on a form that includes a modal to allow users to modify their name and email address. The issue I am facing is that the form sends a request to the server to check for unique values in all cases. While this is appropriate when creating a new record, it becomes problematic when modifying existing information because I want to ignore the current value.
<form>
<input type="text" ng-model="username" unique-url="/test/to/unique" />
</form>
To load this modal form, I change the model value like so:
$scope.showModal = function(row) {
$scope.username = row.name;
showTheFormModal();
}
Below is the directive used:
myAPP.directive('uniqueUrl', ['$http', function($http) {
return {
require: 'ngModel',
link: function(scope, ele, attrs, c) {
var url = attrs.uniqueNameUrl + '?';
scope.$watch(attrs.ngModel, function() {
$http({
method: 'GET',
url: url + $.param({'name': ele.val()})
}).success(function(isUnique,status,headers,cfg) {
var iu = isUnique == 'true' ? true : false;
c.$setValidity('unique', iu);
}).error(function(data,status,headers,cfg) {
c.$setValidity('unique', false);
});
});
}
}
}])