guillaumepotier/Parsley.js — pull request #442 by lencinhaus (2013-11-28 16:56:05+00:00)
Title: Support for asynchronous validation with Promises

Hi,
I love this plugin, and I think it's *almost* perfect. The only thing missing is proper support for asynchronous validation. An attempt at returning `Promise`s during validation has already been made in #402, but I didn't like either the mixed boolean/Deferred return values, so I've written my own version.
I've added 4 new methods to the API:

* `ParsleyField.validateDeferred`
* `ParsleyField.isValidDeferred`
* `ParsleyForm.validateDeferred`
* `ParsleyForm.isValidDeferred`

These methods are the asynchronous counterparts of `validate` and `isValid` in both classes, and they always return a `Promise` instead of a boolean. I've refactored the original `validate` and `isValid` methods in order to use the deferred counterparts under the hood, but they immediately return the computed value. If all validators are synchronous, the behavior is exactly the same as the original version; in fact, all the tests are still passing. If one or more validators are asynchronous, then `validate` and `isValid` return `undefined`, and that's correct because the form/field validity hasn't been actually determined yet.
The `validate` function of validators can now return a `Promise`. If it does still return a boolean or `undefined`, as all built-in validators but `remote` do, then a resolved promise yielding that result is automatically created and chanied to the others. The only built-in validator that I've refactored is `remote`, which now returns a promise that is resolved when the ajax call has been successfully made and the result parsed.
Once all validator/constraint promises have been resolved, the "master" promise returned by `validate` is resolved too, and listeners are notified with the final "verdict" which is a boolean saying if the form/field is valid or not.

I think this is a clean solution for adding asynchronous support to the library without breaking it, as it is totally backward-compatible. In a future major release, it would make sense to only return promises; if you agree, check out this other branch: https://github.com/lencinhaus/Parsley.js/tree/promises. I've refactored it to always return promises directly from `validate` and `isValid`, and I've refactored the tests too in order to use the new `Promise` syntax. They are all passing.

Hope you'll merge this, or implement a similar solution, as I believe async support would really make Parsley the best validation library out there ([jQuery Validation](http://jqueryvalidation.org) doesn't support it either, but it is much more difficult than Parsley to refactor). I'm working on a [Meteor](http://www.meteor.com) project right now, and with this branch I can bind Parsley validation to Meteor server-side methods in a super-clean fashion, which is really, really cool.

P.S. Sorry if I messed up a bit the code format, but I couldn't take the time to configure my IDE to reproduce your code style. I guess you can fix it in a sec though.

Hope this helps, bye!