psaia/react-serial-forms

Async validators

srph opened this issue · 9 comments

srph commented

e.g., checking if username is unique, etc.

Is this possible atm?

psaia commented

Currently async validation is not supported, but certainly an interesting idea. I will absolutely consider it, however, at the moment I'm not sure how it would work and am totally open to suggestions.

Perhaps one idea would be exposing a event dispatcher in the validation module. Something like this:

validators.events.on('asyncError', function(fields) {
  // Here you could call this.forceUpdate();
  // "fields" could also be available for extra information as to what the error is.
});

Let me know what you think.

Also, please note the recent update to custom validators here: #10

srph commented

This is issue #10, I think you mistook it from #5?

We can check if the returned value of the custom validator is a promise.

validation.registerValidator({
  name: 'usernameIsUnique',
  invalid: function(value) {
    return checkUsernameIfValid(value)
      .then(({ data }) => data.error);
  },
  message: 'The field should be larger than 5.'
});

And then we can use a throttle / debounce function to abort if a request is running.

psaia commented

Okay, I like it. I will work to get this fully implemented and tested this weekend.

srph commented

Woah, thanks ❤️ ! Buzz me up for anything, then.

This. Would. Be. Awesome.

If you need any help with user testing before pushing this, hit me up. I'm really keen for this feature for the same reason – async username checking.

psaia commented

I'm sorry for the delay on this guys. The promise implementation is about 70% complete. Work has gotten really busy so I've had to put this to the side but will be back on it soon!

psaia commented

It was a long time coming. Please check out v2.0. This was almost a complete re-write, but for the better. See the updated docs here.

Also please see the updated demo which makes use of a async custom validator.

srph commented

Great, thanks! Sorry if I weren't able to help.

psaia commented

Not a problem, @srph.