I have created a unique custom validator similar to the one available here.
The purpose of my validator is to be versatile enough for use in different contexts. This specific validator checks file type extensions that are inputted, returning true if the observable contains a valid file type. I aim to display a customized error message including the accepted file extensions to notify the user of which types are permissible.
For example:
ko.validation.rules['validateFileTypeExtensions'] = {
validator: function (fileName, validExtensions) {
var isValidExtension = false;
var extension = fileName.split('.').pop();
validExtensions.forEach(function(validExtension){
if(extension == validExtension)
isValidExtension = true;
});
return isValidExtension;
},
// How can I access the validExtensions within the 'message' here?
message: 'Please select a file with an allowed extension ({0}).'
};
ko.validation.registerExtenders();
// The valid file extensions are provided to the validator.
var myCustomObj = ko.observable().extend({ validateFileTypeExtensions: ['doc', 'pdf'] });
In this scenario, the 'message' section lacks access to the validExtensions
variable. Is it possible to retrieve that value within the validator itself?