bouncer icon indicating copy to clipboard operation
bouncer copied to clipboard

Async custom validations

Open backflip opened this issue 6 years ago • 7 comments
trafficstars

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?

backflip avatar May 31 '19 11:05 backflip

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

cferdinandi avatar Jun 07 '19 03:06 cferdinandi

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;

		}
	}
});

backflip avatar Jun 07 '19 20:06 backflip

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?

cferdinandi avatar Jun 07 '19 20:06 cferdinandi

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.

backflip avatar Jun 07 '19 20:06 backflip

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.

cferdinandi avatar Jun 07 '19 21:06 cferdinandi

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: https://github.com/cferdinandi/bouncer/compare/master...backflip:master

backflip avatar Jun 07 '19 21:06 backflip

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.

jacobdubailcadmv avatar Jun 16 '22 23:06 jacobdubailcadmv