简体   繁体   English

多个错误消息,基于另一个字段

[英]Multiple error messages, based on another field

I tried to make the error message dynamically:我尝试动态生成错误消息:

window.Parsley.addValidator('validatorName', {
    requirementType: 'string',
    validateString: function (value) {
    return validateField(value);
    },
    messages: {
        en: 'Invalid ' + someValue
    }
});

But it doesn't change, it set someValue to the first value it was set in the page.但它并没有改变,它将someValue设置为它在页面中设置的第一个值。

How can I change the message based on this value dynamically?如何根据此值动态更改消息?

I would put the logic to get the dynamic value into a function and then call that function in your validator.我会将获取动态值的逻辑放入 function,然后在您的验证器中调用该 function。

function getValue() {
    let returnValue = "";
    // logic to get the value of someValue
    returnValue = someValue
    return returnValue;
}
window.Parsley.addValidator('validatorName', {
    requirementType: 'string',
    validateString: function (value) {
    return validateField(value);
    },
    messages: {
        en: 'Invalid ' + getValue()
    }
});

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

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