Reputation: 1054
With a custom remote validator, how are you supposed to get rid of the error messages?
Using data-parsley-remote-validator='mycustom' on the field will give you an error in the console 'undefined async validator' unless the validator is added on DOM ready i.e not inside another function. However, if it is added on DOM ready, then parsley automatically calls it, which shouldn't happen until submit/change or whatever else you have set.
I can do something like this, but it kind of defeats the object of having parsley call the validator on change:
$('#signUpForm').on('submit', function() {
//add the attribute here to avoid the initial error message
$('#exampleInputEmail1').attr('data-parsley-remote-validator', 'validateEmail');
//then add the custom validator
$('#exampleInputEmail1').parsley()
.addAsyncValidator('validateEmail', function (xhr) {
if(xhr.status == '200') {
return 200;
}
// return the error message if email is taken
else if(xhr.status == '404') {
response = '<ul class="errorlist"><li>That email has already been taken, please try another</li></ul>'
$('#errorResponse').html(response);
}
}, '/api/v1/email/available', { "type": "POST", "dataType": "json", "data": data }
);
});
Upvotes: 0
Views: 1157
Reputation: 1
You can add custom validation message using an AsyncValidator through the function you pass to the validator
function (xhr) {
// if validations are good, return true or a resolved promise
if (xhr.status == 200)
return true;
// Return a rejected promise with the error message you want to display
return Promise.reject("Custom Validation Error")
}
In this way you can set a custom error message per validator and/or in according with the result of the request
Tested with Parsley.js - Version 2.9.2
Upvotes: 0
Reputation: 887
For those who came here for custom remote validator error message in Parsley.js,
You can add data-parsley-remote-message
to the element,
<input type="text" data-parsley-remote-validator="my_remote_validator" data-parsley-remote-message="Custom error message only for remote validation on this element" >
Tested with Parsley.js - Version 2.3.11
Upvotes: 2
Reputation: 79552
Your asynch validator is not supposed to set an error message itself, it should simply return if the value validates or not. The error messages are added with a different API and/or specified as data attributes, check the doc.
Upvotes: 1