jQuery-Form-Validator
jQuery-Form-Validator copied to clipboard
Custom Sanitizer
You've got the ability to create custom validators which is excellent but I'm wondering how you would approach writing a custom sanitiser? We've got a solution we currently apply which is to run our sanitation before we then call the validator programmatically however if we were able to build it into the same workflow, that'd be useful.
I've actually just thought of a solution (and tested) which is to write a custom validator which just runs my sanitation then returns 0. But would still be interested if there's a better way
That probably works. But I guess you would want the sanitation to take place first of all. This is achieved by putting your custom validator first in the data-validation declaration but I dislike the solution anyway because of the following reasons:
- It's rather implicit. One must know that a validation function can also be used as a sanitizer as well as the order of santizers vs the validators.
- The function signature of $.formUtils.addValidator doesn't make sense when it comes to sanitizers.
- All validators will become async in upcoming release. This might not matter but it feels like you would want the santizers to do their thing first and then dispatch the validators.
I guess that you could let the sanitize module provide a way to implement custom sanitizers. Something looking like:
$.formUtils.addSanitizer({
name: 'customSanitation',
sanitationFunction: ($element, value, config) => {
return '';
}
});
Yes exactly. I agree that extending the sanitize functionality in this way would be best. The work around works fine for this project but if I get a chance I'll have a go at writing this.
:+1:
See #643
:+1: