Closed sdhull closed 4 years ago
tldnr: If you use async validator, you definitely should use whenValid
instead of isValid
...
Answer is: yes
if a custom validator responds with an already fullfilled promise. If we ever remote jQuer as a dependency this would probably not be possible though, so maybe it would be best to specify to not rely on this in the doc?
The remote
validator is a complicated case. There is a cache for remote requests (although it is cleared before submit, see #1181), but we call then
on it. IIRC, depending on the version of jQuery you'll get a fullfilled promise, or a promise that will resolve next tick (to be compatible with the official promise spec). In short, I think it depends on your jQuery version.
In any case, you probably want to call whenValid
instead of isValid
and use promises.
Aha thanks. I didn't grok that custom validators should implement their own cache. The docs talk about "the promise" a lot but I thought it was implied that parsley was building & maintaining that cache. Perhaps I was confused because of the cache for remote
.
Right, there are two different things: custom validators may return a promise (which may be dependent on an ajax call / UI to the user / whatever strikes your fancy), while the builtin remote
validator will use a cache.
I'll close this, but PR for documentation always welcome.
What kind of issue is this? (put 'x' between the square brackets)
[ ] Question. This issue tracker is not the place for questions. If you want to ask how to do something, or to understand why something isn't working the way you expect it to, use http://stackoverflow.com/questions/ask . Provide working code, starting from http://codepen.io/marcandre/pen/jqbzyN?editors=101. We monitor the tag
parsley.js
.[x] Bug report. If you’ve found a bug, you must provide a minimal example in a CodePen, starting from http://codepen.io/marcandre/pen/jqbzyN?editors=101 .
[ ] Feature Request. Make sure there's no good way to do what you want first; consider asking on http://stackoverflow.com/questions/ask first.
The docs state
This (to me) seems to indicate that if I call
isValid()
then the promise resolves, then callisValid()
again, that it should returntrue
. However what I've found is that it always returnsnull
if any validations return a promise, and always kicks off that promise again.If I'm reading this wrong, I think the docs could probably be clarified. On the other hand, if then intention is to memoize the result of validation with given inputs, it would seem this does not work. I have added simple
isValid()
assertions in the test that exerciseswhenValidate()
to drive this out.