0
votes

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

2 Answers

2
votes

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

1
votes

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.