javascript parsley.js 如何在带有自定义错误消息的字段上触发错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21148214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:13:14  来源:igfitidea点击:

parsley.js how to trigger an error on a field with a custom error message

javascriptjqueryhtmlcssparsley.js

提问by riccardolardi

It would be great if it was possible to do such thing as

如果可以做这样的事情就太好了

$('input.specific-field').parsley('error', 'this is a custom error message');

...but I guess that isn't possible?

……但我想那是不可能的?

How could I achive such thing?

我怎么能达到这样的目标?

回答by deadwards

Parsley has some built-in ways of adding custom error messages.

Parsley 有一些添加自定义错误消息的内置方法。

var specificField = $('input.specific-field').parsley();
# add the error
window.ParsleyUI.addError(specificField, "myCustomError", 'this is a custom error message');
# remove the error
window.ParsleyUI.removeError(specificField, "myCustomError");

More info here: http://parsleyjs.org/doc/index.html#psly-ui-for-javascript

更多信息:http: //parsleyjs.org/doc/index.html#psly-ui-for-javascript

EDIT: This method is now deprecated(thanks to @giraff).

编辑现在不推荐使用此方法(感谢@giraff)。

回答by giraff

This is how I got it working for Parsley 2.8:

这就是我让它为 Parsley 2.8 工作的方式:

field.parsley().removeError('customValidationId');
field.parsley().addError('customValidationId', {message: "myCustomError"});

http://parsleyjs.org/doc/index.html#psly-ui-for-javascript

http://parsleyjs.org/doc/index.html#psly-ui-for-javascript

Here is a turnkey-solution that I used for showing error from AJAX to fields identified by their id:

这是我用来显示从 AJAX 到由其 ID 标识的字段的错误的交钥匙解决方案:

// Removing errors from previous AJAX call
if ($('.js_error').length) {
    $('.js_error').parsley().removeError('myError');
    $('.js_error').removeClass('.js_error');
}

// Showing errors from current AJAX call
for (var idField in ajaxErrors) {
    var msg = ajaxErrors[idField];
    var field = $('#field_' + idField);
    if (field.length) {
        field.addClass('js_error');
        field.parsley().removeError('myError');
        field.parsley().addError('myError', {message: msg});
    }
}

This solution will not prevent form-submit, though (because it only displays error messages in the UI, the form validation logic is not touched). That's why Parsley.js favors custom validators.

但是,此解决方案不会阻止表单提交(因为它仅在 UI 中显示错误消息,不会触及表单验证逻辑)。这就是Parsley.js 偏爱自定义验证器的原因。

回答by Muhammad Basit

data-parsley-error-message="my message" 

worked for me see http://parsleyjs.org/doc/index.html#ui-for-javascriptfor more info.

对我来说有效,请参阅 http://parsleyjs.org/doc/index.html#ui-for-javascript了解更多信息。

回答by MamaWalter

maybe this:

也许这个:

$('input.specific-field').parsley().UI.manageError({error: 'this is a custom error message'});

回答by johnnypez

Examples above appear to be for Parsley 2.0 I'm stuck with an older version but got custom errors to work with the following.

上面的示例似乎适用于 Parsley 2.0 我坚持使用旧版本但遇到自定义错误以使用以下内容。

el = $('#my_input').parsley();
el.manageErrorContainer(); // set up the error list container
$(el.ulError).empty() // clear any previous errors if you want..
el.addError({error: 'Hey, mind yerself!'});