bouncer
bouncer copied to clipboard
Async custom validations
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?
Hi there... I'm not sure I understand what this means. Can you say more?
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;
}
}
});
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?
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.
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.
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
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.