I'm using ng-messages
to validate my registration form fields, but I have a problem, I can't check if username is taken, until I send registration request to server. Currently I have this type of code:
UserPool.signUp(vm.form.username, vm.form.password, attributes)
.then(function (result) {
$state.go('app.pages_auth_confirm', {
user: result.user
});
})
.catch(function () {
$scope.registerForm.username.$invalid = true;
$scope.registerForm.username.$error.usernameAlreadyExists = true;
});
and HTML:
<input name="username" ng-model="vm.form.username" placeholder="Name" required>
<div ng-messages="registerForm.username.$error" role="alert">
<div ng-message="required">
<span>Username field is required</span>
</div>
<div ng-message="usernameAlreadyExists">
<span>User with this username already exists</span>
</div>
</div>
and this works, but I want to know proper way of implementing that functionality. Registration form has many other fields also and changing some of them manually, for showing error, doesn't seems to be a good idea. I also did some research about validations and messages in angular and found out about custom validation and $asyncValidators
but I can't use that functionality neither because as I understood I'll be needing some kind of API for getting information about used usernames only, but I don't have that kind of possibility, as I already said, I can't check if username is taken, until I send registration request. So can anybody tell me what should I do? What is the proper way for working with this kind of validation?