I have a directive "ensureUniqueValidator" for checking uniqueness of values in database. The function is generic. It is called in the HTML page in the following way:
<input type="email" name="email" ng-model="userCtrl.user.email"
required ensure-unique-validator />
Directive Code:
app.directive('ensureUniqueValidator', [
'$http',
function($http) {
return {
restrict : 'A',
require : 'ngModel',
link : function(scope, ele, attrs, c) {
c.$parsers.push(function(val) {
return $http.get(
'MainServlet.do?method=is' + attrs.name
+ 'unique&' + attrs.name + '='
+ val).then(
function(result) {
console.log(result.data);
c.$setValidity('ensureUniqueValidator',
result.data);
return result.data;//returns true or false
});
});
}
}
} ]);
Problem is, the Form remains valid even if server has returned as False. Looks like, the $setValidity function is not invalidating the Form.
Am I doing something wrong here? Thanks in advance.
c.$asyncValidators, notc.$parsersscript.js) under the Custom Validators link above. Theusernamedirective is similar to what you want. You simply need to return a promise