flackend
3/28/2017 - 5:21 PM

I didn't know if Parsley could handle promise-based validators. While googling I found the annotated source for Parsley's remote.js (http://

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'
    }
});