'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.

How can I change the message based on this value dynamically?



Solution 1:[1]

I would put the logic to get the dynamic value into a function and then call that function in your validator.

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()
    }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mifo