Skip to content

Validate Web Components #2492

Closed
Closed
@dhobi

Description

@dhobi

New feature motivation

With the rise of Web Components and the ElementInternals API it has become quite easy to build custom form elements. Currently, jquery-validation does not support Web Components which means the library does not validate but skip Web Components entirely when they are introduced into an already existing web application (our use case)

New feature description

jquery-validation would need to allow custom tags to be validated as well

New feature implementation

Currently, jquery-validation "only" allows the following elements to be considered in a form:

return $( this.currentForm )
    .find( "input, select, textarea, [contenteditable]" ) // <-- these selectors here
    .not( ":submit, :reset, :image, :disabled" )
    .not( this.settings.ignore )

As I don't know a way to automatically include all "registered Web Components which also implements the ElementInternals API", the idea is to allow the user to define their own selector.

Similar to the element selector above, the selectors for event triggering would also need to be defined by the user:

$( this.currentForm )
    .on( "focusin.validate focusout.validate keyup.validate",
	   :text, [type='password'], [type='file'], select, textarea, ..... // <- here
         )
   .on( "click.validate", "select, option, [type='radio'], [type='checkbox']" // <- here

Extending the validator options:
We could either go with 3 new entries in the settings like:

$.extend( $.validator, {
	defaults: {
            ...
            ignoreTitle: false,
            listenFocusOn: ":text, [type='password'], ..., custom-text" // <-- new
            listenClickOn: "select, option, [type='radio'], [type='checkbox'], custom-text", // <-- new
            elements: "input, select, textarea, [contenteditable], custom-text", // <-- new
            onfocusin: function( element ) {
            ...

or we try to go the "smart way" and provide a single settings entry like:

$.extend( $.validator, {
	defaults: {
            ...
            ignoreTitle: false,
            customElements: [ "custom-text" ] // <-- new
            ...

I opened a PR with the single settings entry variant.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions