Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding highlight/unhighlight events #38

Open
sussexrick opened this issue Apr 11, 2023 · 4 comments · May be fixed by #122
Open

Consider adding highlight/unhighlight events #38

sussexrick opened this issue Apr 11, 2023 · 4 comments · May be fixed by #122

Comments

@sussexrick
Copy link

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?

@haacked
Copy link
Owner

haacked commented Apr 11, 2023

I’d accept a PR for them!

@enkelmedia
Copy link

enkelmedia commented Aug 11, 2023

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?

@dahlbyk
Copy link
Collaborator

dahlbyk commented Aug 11, 2023

Suggest adding public highlight/unhighlight methods to ValidationService, aligned with jQuery Validation (receiving element, errorClass, validClass), extracted from here:

this.swapClasses(input,
this.ValidationInputCssClassName,
this.ValidationInputValidCssClassName);

and here:
this.swapClasses(input,
this.ValidationInputValidCssClassName,
this.ValidationInputCssClassName);

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();

@xsustek
Copy link

xsustek commented Aug 26, 2024

Hi @haacked

I have tried to implement this feature in PR #122.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants