Created
March 28, 2017 17:21
-
-
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/).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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