Skip to content

Instantly share code, notes, and snippets.

@flackend
Created March 28, 2017 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flackend/4236d4409c4103e6d4fd8960513b615a to your computer and use it in GitHub Desktop.
Save flackend/4236d4409c4103e6d4fd8960513b615a to your computer and use it in GitHub Desktop.
I didn't know if Parsley could handle promise-based validators. While googling I found the annotated source for Parsley's remote.js (http://parsleyjs.org/doc/annotated-source/remote.html). All you have to do is return a promise-compatible object (see jQuery.when, https://api.jquery.com/jquery.when/).
Parsley.addValidator('vulnerabilityTitle', {
validateString: (value) => {
// Throws up a loading animation
loading.start();
return new Promise((resolve, reject) => {
VulnerabilityRepository.fetchAll().then((vulnerabilities) => {
let found = vulnerabilities.find((vulnerability) => {
return vulnerability.title == value;
});
loading.stop();
if (found) {
reject();
} else {
resolve();
}
});
});
},
requirementType: 'string',
messages: {
en: 'Title already in use'
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment