Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with async validators #20

Open
zoomisen opened this issue Nov 9, 2016 · 5 comments
Open

Problem with async validators #20

zoomisen opened this issue Nov 9, 2016 · 5 comments
Labels

Comments

@zoomisen
Copy link

zoomisen commented Nov 9, 2016

I'm trying to use an async validator combined with your module, my api return a 200 response if email is unique and a 400 response if it's already in use.

Have you tried using async validators?

I think your module needs some way to notice if validity has changed after an async validator has been deferred.

Or do you have an working example.

The field am testing with looks like this:

<div class="form-group">
		<label for="email">E-mail</label>
		<div class="input-group">
				<div class="input-group-addon"><i class="fa fa-envelope"></i></div>
				<input ng-model="admin.email" id="email" name="email" class="form-control" type="email" required is-unique-admin-email ng-model-options="{debounce: 250}" />
		</div>
</div>
ctrl.$asyncValidators.uniqueAdminEmail = function(modelValue, viewValue)
{
	return $http({
		url: "/validate/unique-admin-email?email=" + value,
		method: "GET",
	})
		.then(function()
		{
			return $q.resolve();
		}, function()
		{
			return $q.reject();
		});
};
@sagrawal31
Copy link
Owner

I'll take a look @zoomisen

@ShaneYu
Copy link
Contributor

ShaneYu commented Jan 15, 2017

After I've finished my changes for feature #24, I am happy to take a look at this one too. But I must say that you would be better off returning a http status of 200 with true meaning it is okay to use and false if not. 400 is a bad request, indicating that what was entered into the request is invalid, but even if the email is already in use, that doesn't make the request invalid. I will look at it as though it is staying as returning 400 or 200.

Out of interest, is this still an issue for you @zoomisen, or have you found a solution now?

@ShaneYu
Copy link
Contributor

ShaneYu commented Jan 15, 2017

@zoomisen: I just noticed and improvement you may like to consider adding to your validator also. prior to making the HTTP call, you may want to check if the modelValue is undefined or empty, if it is, just resolve it. Otherwise when the field is blank, it will still check if the email is unqiue....if it is blank you want it to just think it is valid to prevent the error from showing when it really ought not to; if that makes sense and I've not waffled.

ctrl.$asyncValidators.uniqueAdminEmail = function(modelValue, viewValue) {
        if (!angular.isDefined(modelValue) || modelValue.length === 0) {
                return $q.resolve(); // Consider empty model as valid
        }

	return $http({
		url: "/validate/unique-admin-email?email=" + value,
		method: "GET",
	}).then(function() {
                return $q.resolve();
	}, function() {
		return $q.reject();
	});
};

@sagrawal31
Copy link
Owner

@zoomisen I can reproduce this issue. I'll fix this. @ShaneYu thanks for pitching in!

@sagrawal31 sagrawal31 added the bug label Feb 27, 2017
@Aduer
Copy link

Aduer commented Mar 17, 2017

I managed to make this library working also with asyncValidators
by replacing the watch block @ line 132 in file validation.directive.js (just below the comment

TODO: Find alternative to this watch

with this two watches that takes into account also the $pending property

 $scope.$watch(function () {
			return ngModelController.$error;
        }, function(newValue, oldValue){
			displayOrHideValidationState();
		}, true);
		
        $scope.$watch(function () {
			return ngModelController.$pending;
        }, function(newValue, oldValue){
			displayOrHideValidationState();
		}, true);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants