I have a async validation directive which works fine when, but it depends on two fields to define if a person exists (numId and type), here's the code:
app.directive('personaUnica', function($http, $q){
return{
require:'ngModel',
scope: {
tipo: "=personaUnica"
},
link: function(scope, element, attrs, ctrl){
ctrl.$asyncValidators.personaUnica = function(modelValue, viewValue){
if (ctrl.$isEmpty(modelValue)) {
// valido si es vacio
return $q.when();
}
var defer = $q.defer();
$http.get('/someRESTEndpoint/', {
params:{ identificacion: modelValue, tipo: scope.tipo }
}).then(function(respuesta){
//Person found, not valid
if( respuesta.data.elementoExiste ){
defer.reject('Persona existe.');
}
}, function(respuesta){
//Person not found, resolve promise
if(!respuesta.data.elementoExiste){
defer.resolve();
}
});
return defer.promise;
}
}
}
});
But I dont know how to make the same validation when the other dependant field has changed.
I've read something about require ^form in the directive but in kinda lost.
I've tried to add this block of code
scope.$watch('tipo', function(){
ctrl.$validate();
});
But then I get an infinite $digest loop.
Thanks in advance.