I tried this code:
.directive('uniqueUsername', function (isUsernameAvailable) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
ngModel.$asyncValidators.uniqueName = isUsernameAvailable;
}
};
})
.directive("isMinSix", function () {
return {
restrict: "A",
require: "ngModel",
link: function (scope, element, attributes, ngModel) {
ngModel.$validators.isMinSix = function (modelValue) {
if (modelValue != null && modelValue.length < 6) {
return true;
} else {
return false;
}
}
}
}
})
.factory('isUsernameAvailable', function (appConstant, $q, $http) {
return function (username) {
var deferred = $q.defer();
var url = appConstant.baseUrl + '/api/user/existsByName';
$http({
url: url,
method: "PUT",
data: {
userName: username
}
}).then(function () {
// Found the user, therefore not unique.
deferred.reject("User name is taken");
}, function () {
// User not found, therefore unique!
deferred.resolve();
});
return deferred.promise;
}
})
My problem is that when I add these two directives to an input only and then put a debug point in the checks only one or the other will fire. I cannot get them both to work correctly at the same time:
<input class="inputField"
id="registerUserName"
name="registerUserName"
is-min-six
ng-model="aus.registerUserName"
ng-model-options="{ debounce: 3000 }"
ng-required="true"
placeholder="Username"
type="text"
unique-username
value="" />
Does anyone have any ideas what I may be doing wrong?