简体   繁体   English

parsley.js动态错误消息

[英]parsley.js dynamic error message

I am using parsley.js with the custom validator on a select box. 我在选择框上使用带有自定义验证程序的parsley.js。

I want the error message on the validator to use the currently selected option in the select box + some more text. 我希望验证器上的错误消息使用选择框中的当前选定选项+一些其他文本。

Unfortunately, it seems as if I am unable to change the error message dynamically. 不幸的是,似乎我无法动态更改错误消息。

My code 我的密码

window.Parsley
.addValidator('attachedEmployee', {
    requirementType: 'string',
    validateString: function(value, arg1, arg2, arg3) {
        var employeeID = $("#medarbejder_navn").val();
        //No employee is selected if ID is 1
        if(employeeID == 1)
        {
            //Only shifts which can be made with no employees are "accepted" and "free"
            if(value == "G" || value == "L" || value == "A")
                return true;
            else
            {   
                return false
            }

        }
        else
            return true;
    },
    messages: {
        da: "%s"
    }
});

It would seem the the validator adds the error message as soon as it gets attached, meaning it is locked right from the get go. 验证器似乎在附加错误消息后立即添加它,这意味着它一开始就被锁定了。

Anyone know how to get around this problem? 有人知道如何解决这个问题吗?

Just struggled with this, found the answer in multiple custom error message support per field by using parsley.js 只是为此而苦苦挣扎, 通过使用parsley.js在每个字段中的多个自定义错误消息支持中找到了答案

To change the error message I'm doing this now: 要更改错误消息,我现在正在这样做:

window.Parsley
    .addValidator('atLeast', {
         validateString: function(value, requirement){
             window.Parsley.addMessage('en', 'atLeast','Fill at least ' + requirement + ' input');
             return this.validateAtLeast(Number(requirement)); // custom function
         },
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM