Reputation: 22905
Parsley (2.8) I have a custom validator
window.Parsley.addValidator('password', {
validateString: function(value, req, elem) {
var error = validatePassword(value);
return error === '';
},
messages: {
en: 'Want my custom message based on input value'
}
});
validatePassword(value)
return error message according to value, e.g. special character is missing. So how I can access value or input element inside error message.
Upvotes: 2
Views: 2154
Reputation: 22905
After some struggle, I found a solution. We can't specify callback with arguments to create a dynamic error message but we can add an error message in validation callback where we have access to input value and element as well.
window.Parsley.addMessage(lenguage, validatorName, customErrorMessage)
window.Parsley.addMessage('en', 'password', 'Missing special character')
window.Parsley.addValidator('password', {
validateString: function(value, req, elem) {
// We can add dynamic error message here.
// window.Parsley.addMessage(lenguage, validatorName, customErrorMessage);
var error = validatePassword(value); // validatePassword() returns error message if there are any validation errors otherwise empty string.
window.Parsley.addMessage('en', 'password',error);
return error === '';
}
});
Upvotes: 0
Reputation: 79612
Currently the way to return a "dynamic" error message is by returning a failed promise from your validateString
method. This example uses this technique.
Upvotes: 1