forms icon indicating copy to clipboard operation
forms copied to clipboard

Remote validator.

Open piotrkowalczuk opened this issue 11 years ago • 7 comments

New option that gives posibility to validate form by external application eg by http request to backend server.

piotrkowalczuk avatar May 17 '14 14:05 piotrkowalczuk

Can you elaborate a bit on this one? Remote validations aren't generally possible, because they're async, and form validation usually needs to happen synchronously.

Adding the possibility for an async validation seems like a pretty big feature, so I really want to make sure I understand the use cases first.

ljharb avatar May 17 '14 15:05 ljharb

I use this library in my project and i like it. Our backend architecture has changed. Only one who knows something about validation is backend server, but the frontend server render a html.

Its not a problem that validation is async. I corrected the code and updated the tests

piotrkowalczuk avatar May 17 '14 16:05 piotrkowalczuk

This should be implemented as a form validation hook. All you're doing is calling an additional async function on form validation success and assuming a very specific interface for the response.

Also calling it a "remote" validator is unnecessarily specific. What makes this "remote"?

I suggest an alternate implementation that would be more generalizable.

Created a validators array on the unbound form object (unbound_form.validators). This would be an array of validation callbacks that are invoked when bound_form.validate() is called. By default, there would be one validation callback which mimics the current behavior.

So bound_form.validate would invoke every callback in unbound_form.validators in an async series.

artnez avatar May 24 '14 22:05 artnez

Whats your opinion now?

piotrkowalczuk avatar May 25 '14 12:05 piotrkowalczuk

Cool! Extensible form validators like this will be very useful.

One issue though. IMO the async validator should be dropped entirely. All form validators are async, so the name async is redundant (remote isn't appropriate either due to reasons mentioned above). Everyone is going to handle backend responses differently anyway. The current implementation of the validator assumes everyone handles remote errors like you do.

If there did exist stock form validators, they should conform to some common interfaces used by frameworks like Rails or whatever.

artnez avatar May 25 '14 18:05 artnez

I was little surprised that inside my custom validator I can't make async redis call to check if the account name user is proposing is reserved already and call callback() based on the result inside the redis callback. I think this is a very common use case. I'm not even sure what could be a workaround.

@piotrkowalczuk your PR has some extra files and unnecessary white space fixes. Maybe just create minimal PR that supports async custom validators.

ilkkao avatar Jul 27 '14 12:07 ilkkao

There was a solution. I make async validation now separately and set form.fields.account.error if there's an error before re-rendering the form. It'd still be nice to get a validator based solution if possible.

ilkkao avatar Jul 27 '14 13:07 ilkkao