自定义远程验证器错误消息

时间:2015-05-11 16:11:28

标签: parsley.js

使用自定义远程验证器,您应该如何摆脱错误消息?

在字段上使用data-parsley-remote-validator ='mycustom'将在控制台'undefined async validator'中出现错误,除非在DOM准备好的情况下添加验证器,而不是在另一个函数内。但是,如果在DOM准备就绪时添加它,那么欧芹会自动调用它,直到提交/更改或您设置的任何其他内容都不会发生。

我可以做这样的事情,但它有点打败了欧芹在改变时调用验证器的目标:

$('#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 个答案:

答案 0 :(得分:2)

对于那些来自Parsley.js的自定义远程验证器错误消息的人,

您可以将data-parsley-remote-message添加到元素

<input type="text"  data-parsley-remote-validator="my_remote_validator" data-parsley-remote-message="Custom error message only for remote validation on this element" >

使用 Parsley.js - 版本2.3.11

进行测试

答案 1 :(得分:1)

您的异步验证器本身不应设置错误消息,如果值有效,则应该返回。错误消息使用不同的API添加和/或指定为数据属性,请查看doc。

相关问题