jQuery-Form-Validator icon indicating copy to clipboard operation
jQuery-Form-Validator copied to clipboard

Custom Sanitizer

Open trusteddigital opened this issue 8 years ago • 6 comments

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.

trusteddigital avatar Aug 31 '17 09:08 trusteddigital

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

trusteddigital avatar Aug 31 '17 09:08 trusteddigital

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

victorjonsson avatar Aug 31 '17 14:08 victorjonsson

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.

trusteddigital avatar Aug 31 '17 14:08 trusteddigital

:+1:

victorjonsson avatar Aug 31 '17 14:08 victorjonsson

See #643

trusteddigital avatar Sep 21 '17 11:09 trusteddigital

:+1:

elmatou avatar Nov 08 '17 10:11 elmatou