bouncer icon indicating copy to clipboard operation
bouncer copied to clipboard

Only run custom validations on fields that need them.

Open HaroldPutman opened this issue 5 years ago • 2 comments

Rather than run every custom validators on every field, assign a list of custom validators to a field, and only run those. This seems cleaner for the general case of custom validators. For example if you have a form with 15 inputs, and one of them needs the "isHello" validation, rather than run the validation against every field and have to check each field for a marker attribute, you would add data-bouncer-custom-validations="hello" to the field that needs it. You can specify multiple custom validations in a space-separated list. In the example for password matching I changed the custom attributes from data-bouncer-* to data-custom-* to avoid confusion that matching is a built-in feature of bouncer.

HaroldPutman avatar Jan 07 '19 13:01 HaroldPutman

I'm a bit confused by this one. Custom validations are only run on fields with a provided pattern (which is totally optional). Everything else falls back to default validations for type, required or not, maxlength/minlength, etc.

cferdinandi avatar Jan 07 '19 13:01 cferdinandi

in getErrors you have:

var errors = runValidations(field,settings);

// Check for custom validations
errors = customValidations(field, errors, settings.customValidations, settings);

Which always runs all the registered customValidations. That's why in custom validator to match passwords in the demo page you are checking for the data-bouncer-match attribute before doing any validation:

var selector = field.getAttribute('data-bouncer-match');
if (!selector) return false;

In practice, the sample I added in index.html validates palindromes on an input with no pattern.

HaroldPutman avatar Jan 07 '19 16:01 HaroldPutman