Multiple error messages, based on another field

59 views Asked by At

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?

1

There are 1 answers

0
Mifo On

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