aspnet-client-validation icon indicating copy to clipboard operation
aspnet-client-validation copied to clipboard

Consider adding highlight/unhighlight events

Open sussexrick opened this issue 2 years ago • 4 comments

I would love to replace our jQuery ASP.NET validation with this, but I can't because it's lacking the highlight and unhighlight events that we rely upon.

They're part of jQuery Validation rather than Microsoft's jQuery Unobtrusive Validation, and they let you run extra code at the time a field becomes valid or invalid.

Our use case for this is that we need error messages to be replicated in an Error summary similar to ASP.NET validation summary as well as next to the field, and we need to prepend "Error: " to the page title when there is an error. These changes make error messages more accessible to assistive technology users.

Would you consider adding support for them?

sussexrick avatar Apr 11 '23 13:04 sussexrick

I’d accept a PR for them!

haacked avatar Apr 11 '23 14:04 haacked

Hi!

I was looking for the same thing as we have wrapping divs where we want to set a class when the field is invalid.

@haacked still up for a PR?

Not sure when I can find the time but if I do in the near future, what kind of event would we want? CustomEvent on the form like the validation event or more of a "global" event?

In our use case I think that the best option is a "global" event where we pass the validated field (element) as a argument and it's validation state.

{
valid : true/false,
element : reference to element
}

Any feedback would be appreciated before I get my hands dirty =D

Cheers!

Edit: Another option could also be to pass the event handler as an function-option during bootstrap, that might be cleaner?

enkelmedia avatar Aug 11 '23 20:08 enkelmedia

Suggest adding public highlight/unhighlight methods to ValidationService, aligned with jQuery Validation (receiving element, errorClass, validClass), extracted from here: https://github.com/haacked/aspnet-client-validation/blob/ab9ef9cb640b3f36e9b2a814e6e84c6b409bdc0f/src/index.ts#L1052-L1054 and here: https://github.com/haacked/aspnet-client-validation/blob/ab9ef9cb640b3f36e9b2a814e6e84c6b409bdc0f/src/index.ts#L1087-L1089

Bonus points for also extracting equivalent (un)highlightMessage and (un)highlightSummary the other places we use this.swapClasses().

Usage would be:

const service = new aspnetValidation.ValidationService(console);
service.highlight = function (input, errorClass, validClass) { ... };
service.unhighlight = function (input, errorClass, validClass) { ... };
service.highlightMessage = function (span, errorClass, validClass) { ... };
service.unhighlightMessage = function (span, errorClass, validClass) { ... };
service.highlightSummary = function (el, errorClass, validClass) { ... };
service.unhighlightSummary = function (el, errorClass, validClass) { ... };
service.bootstrap();

dahlbyk avatar Aug 11 '23 20:08 dahlbyk