Reputation: 41
I am using parsley.js with the custom validator on a select box.
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?
Upvotes: 4
Views: 1490
Reputation: 1194
Just struggled with this, found the answer in multiple custom error message support per field by using 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
},
});
Upvotes: 4