【发布时间】:2015-05-11 16:11:28
【问题描述】:
使用自定义远程验证器,您应该如何摆脱错误消息?
在字段上使用 data-parsley-remote-validator='mycustom' 会在控制台“未定义异步验证器”中出现错误,除非验证器已添加到 DOM 就绪,即不在另一个函数中。但是,如果它是在 DOM 就绪时添加的,则 parsley 会自动调用它,这在提交/更改或您设置的任何其他内容之前不会发生。
我可以做这样的事情,但这有点违背让欧芹在更改时调用验证器的目标:
$('#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 }
);
});
【问题讨论】:
标签: parsley.js