Skip to content

Async custom validations #42

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

Open
backflip opened this issue May 31, 2019 · 7 comments
Open

Async custom validations #42

backflip opened this issue May 31, 2019 · 7 comments
Labels
question Further information is requested

Comments

@backflip
Copy link

It would be great to have async custom validations, e.g. for doing remote validation. Would you consider a PR changing the API to being async by default? Or would you recommend another approach?

@cferdinandi
Copy link
Owner

Hi there... I'm not sure I understand what this means. Can you say more?

@cferdinandi cferdinandi added the question Further information is requested label Jun 7, 2019
@backflip
Copy link
Author

backflip commented Jun 7, 2019

Hi Chris
In order for a remote validation to work, we would have to be able to return an async function:

var validate = new Bouncer('form', {
	customValidations: {
		isHello: async function (field) {
			const response = await fetch('/remote', {
				method: 'POST',
				body: JSON.stringify({
					value: field.value
				}),
				headers: {
					'Content-Type': 'application/json',
					'X-Requested-With': 'XMLHttpRequest'
				}});
			const validation = await response.json();

			return !validation.valid;

		}
	}
});

@cferdinandi
Copy link
Owner

Sorry, I meant: what is it you're trying to accomplish? It looks like perhaps using a server to validate values instead of the client?

@backflip
Copy link
Author

backflip commented Jun 7, 2019

Let's say we want to check whether a username is already taken. It saves the user an enormous amount of time getting informed while typing that another name needs to be chosen instead of having to submit the form to the server first.

@cferdinandi
Copy link
Owner

Ah yea, I can see that. My preference would be to not make async a default behavior, as this is really intended to augment native form validation.

I wonder how else I can support that kind of behavior, though. Let me give that a bit of thought.

@backflip
Copy link
Author

backflip commented Jun 7, 2019

Ah, very good point. Would some kind of plugin approach be an option, allowing users to overwrite publicAPIs?

The changes I made for my requirement to work: master...backflip:master

@jacobdubailcadmv
Copy link

Did anything come of this request? I need to compare a form value to a set of metadata that has to be fetched from a server.

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

No branches or pull requests

3 participants