diff --git a/src/core/dom.test.js b/src/core/dom.test.js index a9a3d26bb..af8d72caf 100644 --- a/src/core/dom.test.js +++ b/src/core/dom.test.js @@ -10,6 +10,24 @@ describe("core.dom tests", () => { jest.restoreAllMocks(); }); + describe("jsDOM tests", () => { + it("jsDOM supports input elements outside forms.", () => { + document.body.innerHTML = ` + +
+ +
+ `; + + const outside = document.querySelector("input[name=outside]"); + const inside = document.querySelector("input[name=inside]"); + const form = document.querySelector("form"); + + expect(outside.form).toBe(form); + expect(inside.form).toBe(form); + }); + }); + describe("document_ready", () => { it("calls the callback, once the document is ready.", async () => { let cnt = 0; diff --git a/src/core/events.js b/src/core/events.js index 724c43546..0c481f962 100644 --- a/src/core/events.js +++ b/src/core/events.js @@ -172,7 +172,7 @@ const await_pattern_init = (pattern) => { * A event factory for a bubbling and cancelable generic event. * * @param {string} name - The event name. - * @returns {Event} - Returns a blur event. + * @returns {Event} - Returns a DOM event. */ const generic_event = (name) => { return new Event(name, { @@ -231,6 +231,20 @@ const focus_event = () => { }); }; +const focusin_event = () => { + return new Event("focusin", { + bubbles: true, + cancelable: false, + }); +}; + +const focusout_event = () => { + return new Event("focusout", { + bubbles: true, + cancelable: false, + }); +}; + const input_event = () => { return new Event("input", { bubbles: true, @@ -293,6 +307,8 @@ export default { click_event: click_event, change_event: change_event, focus_event: focus_event, + focusin_event: focusin_event, + focusout_event: focusout_event, input_event: input_event, mousedown_event: mousedown_event, mouseup_event: mouseup_event, diff --git a/src/core/events.test.js b/src/core/events.test.js index 42f03b98a..9be828bd0 100644 --- a/src/core/events.test.js +++ b/src/core/events.test.js @@ -529,6 +529,24 @@ describe("core.events tests", () => { expect(catched).toBe("inner"); }); + it("focusin event", async () => { + outer.addEventListener("focusin", () => { + catched = "outer"; + }); + inner.dispatchEvent(events.focusin_event()); + await utils.timeout(1); + expect(catched).toBe("outer"); + }); + + it("focusout event", async () => { + outer.addEventListener("focusout", () => { + catched = "outer"; + }); + inner.dispatchEvent(events.focusout_event()); + await utils.timeout(1); + expect(catched).toBe("outer"); + }); + it("input event", async () => { outer.addEventListener("input", () => { catched = "outer"; diff --git a/src/pat/auto-suggest/auto-suggest.js b/src/pat/auto-suggest/auto-suggest.js index a322dcb70..1a484cea5 100644 --- a/src/pat/auto-suggest/auto-suggest.js +++ b/src/pat/auto-suggest/auto-suggest.js @@ -122,9 +122,9 @@ export default Base.extend({ const val = $sel2.select2("val"); if (val?.length === 0) { // catches "" and [] - // blur the input field so that pat-validate can kick in when - // nothing was selected. - this.el.dispatchEvent(events.blur_event()); + // focus-out the input field so that pat-validate can kick in + // when nothing was selected. + this.el.dispatchEvent(events.focusout_event()); } }; this.$el.on("select2-close", initiate_empty_check.bind(this)); diff --git a/src/pat/date-picker/date-picker.js b/src/pat/date-picker/date-picker.js index 3c885047c..c283591a3 100644 --- a/src/pat/date-picker/date-picker.js +++ b/src/pat/date-picker/date-picker.js @@ -178,9 +178,9 @@ export default Base.extend({ onSelect: () => this.dispatch_change_event(), onClose: () => { if (this.options.behavior === "styled" && !this.el.value) { - // blur the input field so that pat-validate can kick in when - // nothing was selected. - el.dispatchEvent(events.blur_event()); + // focus-out the input field so that pat-validate can kick + // in when nothing was selected. + el.dispatchEvent(events.focusout_event()); } }, }; diff --git a/src/pat/datetime-picker/datetime-picker.js b/src/pat/datetime-picker/datetime-picker.js index 6c8b13101..45ca48c73 100644 --- a/src/pat/datetime-picker/datetime-picker.js +++ b/src/pat/datetime-picker/datetime-picker.js @@ -117,7 +117,9 @@ export default Base.extend({ for (const input of [this.el_date, this.el_time]) { input.addEventListener("blur", (event) => { if (!el_wrapper.contains(event.relatedTarget)) { - el.dispatchEvent(events.blur_event()); + // focus-out the input field so that pat-validate can kick + // in when nothing was selected. + el.dispatchEvent(events.focusout_event()); } }); } diff --git a/src/pat/datetime-picker/datetime-picker.test.js b/src/pat/datetime-picker/datetime-picker.test.js index 4998c3ba7..d3fe15767 100644 --- a/src/pat/datetime-picker/datetime-picker.test.js +++ b/src/pat/datetime-picker/datetime-picker.test.js @@ -285,4 +285,29 @@ describe("pat-datetime-picker", function () { expect(end.value).toBe("2021-03-15T12:34"); }, ); + + it("dispatches a bubbling focusout event on the original input when focus leaves the widget", async () => { + // We mocking as if we're not supporting input type date. + jest.spyOn(utils, "checkInputSupport").mockImplementation(() => false); + + document.body.innerHTML = ` + + + `; + const el = document.querySelector(".pat-datetime-picker"); + pattern.init(el); + await utils.timeout(1); // wait a tick for async to settle. + + const targets = []; + document.addEventListener("focusout", (e) => targets.push(e.target)); + + const el_time = document.querySelector("input[type=time]"); + el_time.dispatchEvent( + new FocusEvent("blur", { + relatedTarget: document.querySelector("#outside"), + }), + ); + + expect(targets).toContain(el); + }); }); diff --git a/src/pat/validation/documentation.md b/src/pat/validation/documentation.md index a3dc74fdd..7961ff018 100644 --- a/src/pat/validation/documentation.md +++ b/src/pat/validation/documentation.md @@ -20,6 +20,7 @@ These extra validation rules are: - Equality checking between two fields (e.g. password confirmation). - Date and datetime validation for before and after a given date or another input field. +- Minimum and maximum number of checked, selected or filled-out fields. Most useful for checkboxes, but also works for text-inputs, selects and other form elements. ### HTML form validation framework integration. @@ -88,10 +89,10 @@ In addition both the input element and its label will get an `warning` class. ``` -Checkboxes and radio buttons are treated differently: if they are contained in a fieldset with class `checklist` error messages are added at the end of the fieldset. +Checkboxes and radio buttons are treated differently: The error message is always set after the last element of the inputs with the same name - or of the group of inputs, see [Minimum and maximum number of values](#minimum-and-maximum-number-of-values). If that input is wrapped in a label, the error message is set after the label. ```html -
+
@@ -109,6 +110,53 @@ ValidationPattern.prototype.error_template = (message) => `${message}`; ``` + +### Minimum and maximum number of values + +With `min-values` and `max-values` you can define how many inputs of a group must be checked, selected or filled out. +This is most useful for checkboxes, but also works for text inputs, selects and other form elements. + +A group is defined either: + +- by the inputs with the same name, if the options are defined on the input itself, or +- by all inputs within a wrapper element, if the options are defined on a wrapper element like a fieldset. + In this case the inputs do not need to share the same name. + +```html +
+ + + +
+``` + +All inputs of a group share the same validation state and the error message is placed after the last input of the group. +Therefore a wrapper element should only contain the inputs of the group. +If `min-values` or `max-values` is defined on the form itself, all inputs of the form form one group. +Define `min-values` and `max-values` on the same element. + + +### Form elements outside the form + +Input elements outside of form elements are fully supported. +pat-validation can handle structures like these: + +```html + +
+
+ +``` + +More information on the `form` attribute can be found at [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#form). + + +### Dynamic forms + +pat-validation supports dynamic forms where form elements are added after the Pattern was initialized. +There is no need to re-initialize the pattern or to dispatch a special event. + + ### Options reference > **_NOTE:_** The form inputs must have a `name` attribute, otherwise the @@ -130,7 +178,11 @@ ValidationPattern.prototype.error_template = (message) => | message-number | The error message for numbers. | This value must be a number. | String | | message-required | The error message for required fields. | This field is required. | String | | message-equality | The error message for fields required to be equal | is not equal to %{attribute} | String | +| message-min-values | The error message when the minimum number of checked, selected or filled-out fields has not been reached. | You need to select at least %{count} item(s). | String | +| message-max-values | The error message when the maximum number of checked, selected or filled-out fields has been exceeded. | You need to select at most %{count} item(s). | String | | equality | Field-specific extra rule. The name of another input this input should equal to (useful for password confirmation). | | String | | not-after | Field-specific extra rule. A lower time limit restriction for date and datetime fields. | | CSS Selector or a ISO8601 date string. | | not-before | Field-specific extra rule. An upper time limit restriction for date and datetime fields. | | CSS Selector or a ISO8601 date string. | +| min-values | Minimum number of checked, selected or filled out form elements of a group (same name or wrapper element). | null | Integer (or null) | +| max-values | Maximum number of checked, selected or filled out form elements of a group (same name or wrapper element). | null | Integer (or null) | | delay | Time in milliseconds before validation starts to avoid validating while typing. | 100 | Integer | diff --git a/src/pat/validation/index.html b/src/pat/validation/index.html index 595844667..d69bb743c 100644 --- a/src/pat/validation/index.html +++ b/src/pat/validation/index.html @@ -115,8 +115,6 @@ yellow
-
- @@ -250,6 +247,166 @@
+

Demo with max-values / min-values support

+
+
+ Multi select + +
+ +
+ Multiple checkboxes + + + + +
+ +
+ Demo with mixed inputs and max/min values support. +
+ +
+
+ + + + +
+
+ + + +
+
+ +
+ Demo with a group of differently named checkboxes (min 1, max 2). + + + +
+
+ + +
+
+
{ + async (event) => { // On submit, check all. // Immediate, non-debounced check with submit. Otherwise submit // is not cancelable. - for (const input of this.inputs) { - logger.debug("Checking input for submit", input, e); - await this.check_input({ input: input, event: e }); - } + await this.validate_all(event); }, // Make sure this event handler is run early, in the capturing // phase in order to be able to cancel later non-capturing submit // events. - { capture: true } + { capture: true }, ); - this.initialize_inputs(); - $(this.el).on("pat-update", () => { - this.initialize_inputs(); - }); + // Input debouncer map: + // - key: input element + // - value: debouncer function + // 1) We want do debounce the validation checks to avoid validating + // while typing. + // 2) We want to debounce the input events individually, so that we can + // do multiple checks in parallel and show multiple errors at once. + const input_debouncer_map = new Map(); + const debounce_filter = (e) => { + const input = e.target; + if (input?.form !== this.form || !this.inputs.includes(input)) { + // Ignore events from other forms or from elements which are + // not inputs. + return; + } + + if (!input_debouncer_map.has(input)) { + // Create a new cancelable debouncer for this input. + input_debouncer_map.set( + input, + utils.debounce((e) => { + logger.debug("Checking input for event", input, e); + this.check_input({ input: input, event: e }); + }, this.options.delay), + ); + } + + // Get the debouncer for this input. + const debouncer = input_debouncer_map.get(input); + // Debounce the validation check. + debouncer(e); + }; + + events.add_event_listener( + document, + "input", + `pat-validation--${this.uuid}--input--validator`, + (e) => debounce_filter(e), + ); + + events.add_event_listener( + document, + "change", + `pat-validation--${this.uuid}--change--validator`, + (e) => debounce_filter(e), + ); + + events.add_event_listener( + document, + "focusout", + `pat-validation--${this.uuid}--focusout--validator`, + (e) => debounce_filter(e), + ); // Set ``novalidate`` attribute to disable the browser's validation // bubbles but not disable the validation API. - this.el.setAttribute("novalidate", ""); + this.form.setAttribute("novalidate", ""); } - initialize_inputs() { - this.inputs = [ - ...this.el.querySelectorAll("input[name], select[name], textarea[name]"), - ]; - this.disabled_elements = [ - ...this.el.querySelectorAll(this.options.disableSelector), - ]; - - for (const [cnt, input] of this.inputs.entries()) { - // Cancelable debouncer. - const debouncer = utils.debounce((e) => { - logger.debug("Checking input for event", input, e); - this.check_input({ input: input, event: e }); - }, this.options.delay); - - events.add_event_listener( - input, - "input", - `pat-validation--input-${input.name}--${cnt}--validator`, - (e) => debouncer(e) - ); - events.add_event_listener( - input, - "change", - `pat-validation--change-${input.name}--${cnt}--validator`, - (e) => debouncer(e) - ); - events.add_event_listener( - input, - "blur", - `pat-validation--blur-${input.name}--${cnt}--validator`, - (e) => debouncer(e) - ); + destroy() { + // Remove the delegated event listeners from the document. + events.remove_event_listener(document, `pat-validation--${this.uuid}--*`); + super.destroy(); + } + + get inputs() { + // Return all inputs elements + return [...this.form.elements].filter((input) => + input.matches("input[name], select[name], textarea[name]"), + ); + } + + get disableables() { + // Return all elements, which should be disabled when there are errors. + return [...this.form.elements].filter((input) => + input.matches(this.options.disableSelector), + ); + } + + group_root(input) { + // Return the closest ancestor element of an input - up to and + // including the form - which itself defines `min-values` or + // `max-values`. All inputs within this element form a group. + // Return null if there is no such element or if the input itself + // defines these options. + for (let el = input; el; el = el.parentElement) { + if (el.hasAttribute(parser.attribute)) { + // Parse only the options defined on this element. + const options = parser.parse(el, {}, false, false); + if ( + (options.minValues ?? null) !== null || + (options.maxValues ?? null) !== null + ) { + return el === input ? null : el; + } + } + if (el === this.form) { + break; + } + } + return null; + } + + siblings(input) { + // Get all inputs which belong to the same group as the given input. + // If a wrapper element defines `min-values` or `max-values`, all + // inputs within that wrapper form a group. Otherwise all inputs with + // the same name form a group. + const group_root = this.group_root(input); + if (group_root === this.form) { + return this.inputs; } + if (group_root) { + return this.inputs.filter((_input) => group_root.contains(_input)); + } + return this.inputs.filter((_input) => _input.name === input.name); } - async check_input({ input, event, stop = false }) { + async validate_all(event) { + // Check all inputs. + for (const input of this.inputs) { + await this.check_input({ input: input, event: event, stop: true }); + } + } + + async check_input({ + input, // Input to check. + event = null, // Optional event which triggered the check. + stop = false, // Stop flag to avoid infinite loops. Will not check dependent inputs. + }) { if (input.disabled) { // No need to check disabled inputs. return; @@ -132,8 +222,8 @@ class Pattern extends BasePattern { if ( input_options.equality && - this.el.querySelector(`[name=${input_options.equality}]`)?.value !== - input.value + this.inputs.find((_input) => _input.name === input_options.equality) + ?.value !== input.value ) { const message = input_options.message.equality || @@ -143,7 +233,12 @@ class Pattern extends BasePattern { msg: message, attribute: input_options.equality, }); - } else if (input_options.not.after || input_options.not.before) { + } + + if ( + !validity_state.customError && // No error from previous checks. + (input_options.not.after || input_options.not.before) + ) { const msg = input_options.message.date || input_options.message.datetime; const msg_default_not_before = "The date must be after %{attribute}"; const msg_default_not_after = "The date must be before %{attribute}"; @@ -202,7 +297,7 @@ class Pattern extends BasePattern { // with a single space. msg_attr = not_after_el?.labels?.[0]?.textContent.replace( /\s\s+/g, // replace all whitespace - " " // with a single space + " ", // with a single space ); msg_attr = msg_attr || not_after_el.name; } @@ -223,7 +318,7 @@ class Pattern extends BasePattern { // with a single space. msg_attr = not_before_el?.labels?.[0]?.textContent.replace( /\s\s+/g, // replace all whitespace - " " // with a single space + " ", // with a single space ); msg_attr = msg_attr || not_before_el.name; } @@ -247,10 +342,55 @@ class Pattern extends BasePattern { } } - if (!validity_state.customError) { - // No error to handle. Return. - this.remove_error(input, true); - return; + if ( + !validity_state.customError && // No error from previous checks. + (input_options.minValues || input_options.maxValues) + ) { + const min_values = + input_options.minValues === null + ? null + : parseInt(input_options.minValues, 10); + const max_values = + input_options.maxValues === null + ? null + : parseInt(input_options.maxValues, 10); + + let number_values = 0; + for (const _input of this.siblings(input)) { + // Check if checkboxes or radios are checked ... + if (_input.type === "checkbox" || _input.type === "radio") { + if (_input.checked) { + number_values++; + } + continue; + } + + // Select, if select is selected. + if (_input.tagName === "SELECT") { + number_values += _input.selectedOptions.length; + continue; + } + + // For the rest a value must be set. + if (_input.value === 0 || _input.value) { + number_values++; + } + } + + if (max_values !== null && number_values > max_values) { + this.set_error({ + input: input, + msg: input_options.message["max-values"], + max: max_values, + }); + } + if (min_values !== null && number_values < min_values) { + this.set_error({ + input: input, + msg: input_options.message["min-values"], + min: min_values, + }); + } } } else { // Default error cases with custom messages. @@ -312,6 +452,12 @@ class Pattern extends BasePattern { } } + if (validity_state.valid) { + // No error to handle. Remove eventual error and return. + this.remove_error({ input }); + return; + } + if (event?.type === "submit") { // Do not submit in error case and prevent other handlers to take action. event.preventDefault(); @@ -333,15 +479,18 @@ class Pattern extends BasePattern { if (attribute) { msg = msg.replace(/%{attribute}/g, attribute); } - if (min) { + if (min !== null) { msg = msg.replace(/%{count}/g, min); } - if (max) { + if (max !== null) { msg = msg.replace(/%{count}/g, max); } msg = msg.replace(/%{value}/g, JSON.stringify(input.value)); - input.setCustomValidity(msg); + // Set the error state the input itself and on all siblings, if any. + for (const _input of this.siblings(input)) { + _input.setCustomValidity(msg); + } // Store the error message on the input. // Hidden inputs do not participate in validation but we need this // (e.g. styled date input). @@ -352,26 +501,34 @@ class Pattern extends BasePattern { } } - remove_error(input, all_of_group = false, skip_event = false) { - // Remove error message and related referencesfrom input. + remove_error({ + input, + all_of_group = true, + clear_state = true, + skip_event = false, + }) { + // Remove error message and related references from input. let inputs = [input]; if (all_of_group) { // Get all inputs with the same name - e.g. radio buttons, checkboxes. - inputs = this.inputs.filter((it) => it.name === input.name); + inputs = this.siblings(input); } - for (const it of inputs) { - const error_node = it[KEY_ERROR_EL]; - it[KEY_ERROR_EL] = null; + for (const _input of inputs) { + if (clear_state) { + this.set_error({ input: _input, msg: "", skip_event: true }); + } + const error_node = _input[KEY_ERROR_EL]; + _input[KEY_ERROR_EL] = null; error_node?.remove(); } // disable selector - if (this.el.checkValidity()) { - for (const it of this.disabled_elements) { - if (it.disabled) { - it.removeAttribute("disabled"); - it.classList.remove("disabled"); + if (this.form.checkValidity()) { + for (const _input of this.disableables) { + if (_input.disabled) { + _input.removeAttribute("disabled"); + _input.classList.remove("disabled"); } } } @@ -383,12 +540,17 @@ class Pattern extends BasePattern { async set_error_message(input) { // First, remove the old error message. - this.remove_error(input, false, true); + this.remove_error({ + input, + all_of_group: false, + clear_state: false, + skip_event: true, + }); // Do not set a error message for a input group like radio buttons or // checkboxes where one has already been set. - const inputs = this.inputs.filter((it) => it.name === input.name); - if (inputs.length > 1 && inputs.some((it) => !!it[KEY_ERROR_EL])) { + const inputs = this.siblings(input); + if (inputs.length > 1 && inputs.some((_input) => !!_input[KEY_ERROR_EL])) { // error message for input group already set. return; } @@ -400,47 +562,40 @@ class Pattern extends BasePattern { // executed within the web page context. const dompurify = (await import("dompurify")).default; const validation_message = dompurify.sanitize( - input.validationMessage || input[KEY_ERROR_MSG] + input.validationMessage || input[KEY_ERROR_MSG], ); const error_node = dom.create_from_string( - this.error_template(validation_message) + this.error_template(validation_message), ).firstChild; - let fieldset; - if (input.type === "radio" || input.type === "checkbox") { - fieldset = input.closest("fieldset.pat-checklist"); - } - if (fieldset) { - fieldset.append(error_node); - } else { - input.after(error_node); - } + // Put error message after the erroneous input or - in case of + // multiple inputs with the same name - after the last one of the + // group. If the input is wrapped in a label, put the error message + // after the label. + const last_input = inputs.pop(); + (last_input.closest("label") || last_input).after(error_node); input[KEY_ERROR_EL] = error_node; let did_disable = false; - for (const it of this.disabled_elements) { + for (const _input of this.disableables) { // Disable for melements if they are not already disabled and which // do not have set the `formnovalidate` attribute, e.g. // ``. - if (!it.disabled && !it.formNoValidate) { + if (!_input.disabled && !_input.formNoValidate) { did_disable = true; - it.setAttribute("disabled", "disabled"); - it.classList.add("disabled"); - logger.debug("Disable element", it); + _input.setAttribute("disabled", "disabled"); + _input.classList.add("disabled"); + logger.debug("Disable element", _input); } } - // Do an initial check of the whole form when a form element (e.g. the - // submit button) was disabled. We want to show the user all possible - // errors at once and after the submit button is disabled there is no - // way to check the whole form at once. ... well we also do not want to - // check the whole form when one input was changed.... + // Check the whole form when a form element (e.g. the submit button) + // was disabled. We want to show the user all possible errors at once + // and after the submit button is disabled there is no way for the user + // to check the whole form at once. if (did_disable) { - logger.debug("Checking whole form after element was disabled."); - for (const _input of this.inputs.filter((it) => it !== input)) { - await this.check_input({ input: _input, stop: true }); - } + await this.validate_all(); } } diff --git a/src/pat/validation/validation.test.js b/src/pat/validation/validation.test.js index 4f34fe9d1..67b742f63 100644 --- a/src/pat/validation/validation.test.js +++ b/src/pat/validation/validation.test.js @@ -205,7 +205,7 @@ describe("pat-validation", function () { await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "This value must be an integer." + "This value must be an integer.", ); expect(el.querySelector("#form-buttons-create").disabled).toBe(true); @@ -240,7 +240,7 @@ describe("pat-validation", function () { await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("div.validation-error").length).toBe(1); expect(el.querySelectorAll("div.validation-error")[0].textContent).toBe( - "need this" + "need this", ); }); @@ -478,7 +478,7 @@ describe("pat-validation", function () { const instance = new Pattern(el); await events.await_pattern_init(instance); - document.querySelector("[name=i1]").dispatchEvent(events.blur_event()); + document.querySelector("[name=i1]").dispatchEvent(events.focusout_event()); await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("em.warning").length).toBe(2); @@ -499,7 +499,7 @@ describe("pat-validation", function () { const instance = new Pattern(el); await events.await_pattern_init(instance); - document.querySelector("[name=i1]").dispatchEvent(events.blur_event()); + document.querySelector("[name=i1]").dispatchEvent(events.focusout_event()); await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("em.warning").length).toBe(1); @@ -539,6 +539,283 @@ describe("pat-validation", function () { expect(event.detail.dom).toBe(el); expect(event.detail.action).toBe("valid"); }); + + it("1.22 - Supports validation of inputs outside forms.", async function () { + document.body.innerHTML = ` + + +
+ + `; + const form = document.querySelector(".pat-validation"); + const input = document.querySelector("[name=outside]"); + const button = document.querySelector("button"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + input.value = ""; + input.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(document.querySelectorAll("em.warning").length).toBe(1); + // Skip, as jsDOM does not support the `:invalid` or `:valid` + // pseudo selectors on forms. + //expect(form.matches(":invalid")).toBe(true); + expect(input.matches(":invalid")).toBe(true); + expect(button.matches(":disabled")).toBe(true); + }); + + it("1.22.1 - Supports equality checks with inputs outside forms.", async function () { + document.body.innerHTML = ` + +
+ +
+ `; + const form = document.querySelector(".pat-validation"); + const inside = document.querySelector("[name=inside]"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + inside.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(inside.matches(":invalid")).toBe(true); + expect(document.querySelector("em.warning").textContent).toBe("not equal"); + + inside.value = "secret"; + inside.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(inside.matches(":invalid")).toBe(false); + expect(document.querySelectorAll("em.warning").length).toBe(0); + }); + + it("1.23 - Puts the warning at the end of same-name inputs.", async function () { + document.body.innerHTML = ` +
+ + + +
+ `; + const form = document.querySelector(".pat-validation"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + form.dispatchEvent(events.submit_event()); + await utils.timeout(1); // wait a tick for async to settle. + + const warning = form.querySelector("em.warning"); + expect(warning).toBeTruthy(); + expect(warning.matches("input:nth-child(3) + em.warning")).toBe(true); + }); + + it("1.23.1 - Puts the warning after the label of label-wrapped inputs.", async function () { + document.body.innerHTML = ` +
+
+ + +
+
+ `; + const form = document.querySelector(".pat-validation"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + form.dispatchEvent(events.submit_event()); + await utils.timeout(1); // wait a tick for async to settle. + + const warnings = form.querySelectorAll("em.warning"); + expect(warnings.length).toBe(1); + expect(warnings[0].parentElement.tagName).toBe("FIELDSET"); + expect(warnings[0].matches("label:nth-child(2) + em.warning")).toBe(true); + }); + + it("1.24 - Supports dynamic forms.", async function () { + document.body.innerHTML = ` +
+ +
+ `; + const form = document.querySelector(".pat-validation"); + const input1 = form.querySelector("[name=input1]"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + form.dispatchEvent(events.submit_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(document.querySelectorAll("em.warning").length).toBe(1); + + input1.value = "ok"; + input1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(document.querySelectorAll("em.warning").length).toBe(0); + + const input2 = document.createElement("input"); + input2.name = "input2"; + input2.required = true; + form.appendChild(input2); + + form.dispatchEvent(events.submit_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(document.querySelectorAll("em.warning").length).toBe(1); + expect(document.querySelector("em.warning").matches("input[name=input2] + em.warning")).toBe(true); + }); + + it("1.24.1 - Passes the triggering event to check_input.", async function () { + document.body.innerHTML = ` +
+ +
+ `; + const form = document.querySelector(".pat-validation"); + const input1 = form.querySelector("[name=input1]"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + const spy = jest.spyOn(instance, "check_input"); + const event = events.change_event(); + input1.dispatchEvent(event); + await utils.timeout(1); // wait a tick for async to settle. + + expect(spy).toHaveBeenCalledTimes(1); + expect(spy.mock.calls[0][0].input).toBe(input1); + expect(spy.mock.calls[0][0].event).toBe(event); + }); + + it("1.24.2 - Removes the document event listeners on destroy.", async function () { + document.body.innerHTML = ` +
+ +
+ `; + const form = document.querySelector(".pat-validation"); + const input1 = form.querySelector("[name=input1]"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + instance.destroy(); + + const spy = jest.spyOn(instance, "check_input"); + input1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(spy).not.toHaveBeenCalled(); + expect(document.querySelectorAll("em.warning").length).toBe(0); + }); + + it("1.25 - Can combine multiple custom validation rules.", async function () { + document.body.innerHTML = ` +
+
+ + +
+ +
+ `; + const form = document.querySelector(".pat-validation"); + const input1 = document.querySelector("#i1"); + const input2 = document.querySelector("#i2"); + const input3 = document.querySelector("#i3"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + input1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + // form is invalid due to input1's required attribute + expect(input1.matches(":invalid")).toBe(true); + expect(document.querySelectorAll("em.warning").length).toBe(1); + expect(document.querySelectorAll("em.warning")[0].textContent).toBe( + "is required" + ); + + input1.value = "ok"; + input1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + // form is invalid due to input1's equality constraint + expect(input1.matches(":invalid")).toBe(true); + expect(document.querySelectorAll("em.warning").length).toBe(1); + expect(document.querySelectorAll("em.warning")[0].textContent).toBe( + "not equal" + ); + + input3.value = "ok"; + input1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + // form is invalid due to input1's equality constraint + expect(input1.matches(":invalid")).toBe(true); + expect(document.querySelectorAll("em.warning").length).toBe(1); + expect(document.querySelectorAll("em.warning")[0].textContent).toBe( + "too few" + ); + + input2.value = "ok"; + form.dispatchEvent(events.submit_event()); + await utils.timeout(1); // wait a tick for async to settle. + + // form is now valid. + expect(input1.matches(":invalid")).toBe(false); + expect(document.querySelectorAll("em.warning").length).toBe(0); + }); + + it("1.25.1 - Does not overwrite a previous custom validation error.", async function () { + document.body.innerHTML = ` +
+ + +
+ `; + const form = document.querySelector(".pat-validation"); + const input_a = form.querySelector("[name=a]"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + input_a.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(input_a.matches(":invalid")).toBe(true); + expect(form.querySelectorAll("em.warning").length).toBe(1); + expect(form.querySelector("em.warning").textContent).toBe("not equal"); + }); }); describe("2 - required inputs", function () { @@ -636,7 +913,7 @@ describe("pat-validation", function () { expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelector("em.warning").textContent).toBe( - "I'm sorry Dave, I can't let you do that." + "I'm sorry Dave, I can't let you do that.", ); }); @@ -683,7 +960,7 @@ describe("pat-validation", function () { expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelector("em.warning").textContent).toBe( - "This value must be a valid email address." + "This value must be a valid email address.", ); inp.value = "person@mail.com"; @@ -721,7 +998,7 @@ describe("pat-validation", function () { expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelector("em.warning").textContent).toBe( - "This value must be greater than or equal to 5" + "This value must be greater than or equal to 5", ); inp_min.value = 6; @@ -738,7 +1015,7 @@ describe("pat-validation", function () { expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelector("em.warning").textContent).toBe( - "This value must be less than or equal to 5" + "This value must be less than or equal to 5", ); inp_max.value = 5; @@ -861,7 +1138,7 @@ describe("pat-validation", function () { expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "The value must be an integer." + "The value must be an integer.", ); inp.value = 5; @@ -891,7 +1168,7 @@ describe("pat-validation", function () { expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "The value must be an integer." + "The value must be an integer.", ); inp.value = 5; @@ -1075,7 +1352,7 @@ describe("pat-validation", function () { await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "The date must be after 2011-11-11" + "The date must be after 2011-11-11", ); inp.value = "2023-02-23"; @@ -1083,7 +1360,7 @@ describe("pat-validation", function () { await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "The date must be before 2022-02-22" + "The date must be before 2022-02-22", ); inp.value = "2022-01-01"; @@ -1130,11 +1407,11 @@ describe("pat-validation", function () { expect(el.querySelectorAll("em.warning").length).toBe(2); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "The date must be before woo date" + "The date must be before woo date", ); expect(el.querySelectorAll("em.warning")[1].textContent).toBe( - "The date must be after ye date" + "The date must be after ye date", ); }); @@ -1171,11 +1448,11 @@ describe("pat-validation", function () { expect(el.querySelectorAll("em.warning").length).toBe(2); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "The date must be before date2" + "The date must be before date2", ); expect(el.querySelectorAll("em.warning")[1].textContent).toBe( - "The date must be after date1" + "The date must be after date1", ); }); @@ -1221,10 +1498,10 @@ describe("pat-validation", function () { await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("em.warning").length).toBe(2); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "The start date must on or before the end date." + "The start date must on or before the end date.", ); expect(el.querySelectorAll("em.warning")[1].textContent).toBe( - "The end date must on or before the start date." + "The end date must on or before the start date.", ); // Fulfill the before/after constraint - same date @@ -1241,10 +1518,10 @@ describe("pat-validation", function () { await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("em.warning").length).toBe(2); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "The start date must on or before the end date." + "The start date must on or before the end date.", ); expect(el.querySelectorAll("em.warning")[1].textContent).toBe( - "The end date must on or before the start date." + "The end date must on or before the start date.", ); // Fulfill the before/after constraint - start before end @@ -1315,7 +1592,7 @@ describe("pat-validation", function () { expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "This field is required" + "This field is required", ); }); @@ -1361,10 +1638,10 @@ describe("pat-validation", function () { await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("em.warning").length).toBe(2); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "The start date/time must on or before the end date/time." + "The start date/time must on or before the end date/time.", ); expect(el.querySelectorAll("em.warning")[1].textContent).toBe( - "The end date/time must on or before the start date/time." + "The end date/time must on or before the start date/time.", ); // Fulfill the before/after constraint - same date @@ -1381,10 +1658,10 @@ describe("pat-validation", function () { await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("em.warning").length).toBe(2); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "The start date/time must on or before the end date/time." + "The start date/time must on or before the end date/time.", ); expect(el.querySelectorAll("em.warning")[1].textContent).toBe( - "The end date/time must on or before the start date/time." + "The end date/time must on or before the start date/time.", ); // Fulfill the before/after constraint - start before end @@ -1453,7 +1730,7 @@ describe("pat-validation", function () { expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "This field is required" + "This field is required", ); inps[0].checked = true; @@ -1499,7 +1776,7 @@ describe("pat-validation", function () { await utils.timeout(1); // wait a tick for async to settle. expect(el.querySelectorAll("em.warning").length).toBe(1); expect(el.querySelectorAll("em.warning")[0].textContent).toBe( - "I would like this to be equal to password" + "I would like this to be equal to password", ); expect(el.querySelector("#form-buttons-create").disabled).toBe(true); @@ -1623,7 +1900,7 @@ describe("pat-validation", function () { // The text content should contain the message without HTML tags expect(warning_text).toContain( - "This field is required and must be filled out." + "This field is required and must be filled out.", ); // The innerHTML should contain the safe HTML (DOMPurify allows by default) @@ -1751,4 +2028,406 @@ describe("pat-validation", function () { expect(document.querySelectorAll("script").length).toBe(0); }); }); + + describe("9 - min/max value validation", function () { + it("9.1 - validate min and max number of checked checkboxes", async function () { + document.body.innerHTML = ` +
+
+ + + + +
+
+ `; + + const form = document.querySelector("form"); + const check1 = form.querySelector("[name=check][value='1']"); + const check2 = form.querySelector("[name=check][value='2']"); + const check3 = form.querySelector("[name=check][value='3']"); + const check4 = form.querySelector("[name=check][value='4']"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + check1.checked = true; + check1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(check1.matches(":invalid")).toBe(true); + expect(check2.matches(":invalid")).toBe(true); + expect(check3.matches(":invalid")).toBe(true); + expect(check4.matches(":invalid")).toBe(true); + expect(form.querySelectorAll("em.warning").length).toBe(1); + expect(form.querySelectorAll("em.warning")[0].textContent).toBe( + "Please select at least 2 options" + ); + + check2.checked = true; + check2.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(check1.matches(":invalid")).toBe(false); + expect(check2.matches(":invalid")).toBe(false); + expect(check3.matches(":invalid")).toBe(false); + expect(check4.matches(":invalid")).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(0); + + check3.checked = true; + check3.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(check1.matches(":invalid")).toBe(false); + expect(check2.matches(":invalid")).toBe(false); + expect(check3.matches(":invalid")).toBe(false); + expect(check4.matches(":invalid")).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(0); + + check4.checked = true; + check4.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(check1.matches(":invalid")).toBe(true); + expect(check2.matches(":invalid")).toBe(true); + expect(check3.matches(":invalid")).toBe(true); + expect(check4.matches(":invalid")).toBe(true); + expect(form.querySelectorAll("em.warning").length).toBe(1); + expect(form.querySelectorAll("em.warning")[0].textContent).toBe( + "Please select at most 3 options" + ); + + check4.checked = false; + check4.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(check1.matches(":invalid")).toBe(false); + expect(check2.matches(":invalid")).toBe(false); + expect(check3.matches(":invalid")).toBe(false); + expect(check4.matches(":invalid")).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(0); + }); + + it("9.2 - validate min and max number with mixed inputs", async function () { + document.body.innerHTML = ` +
+
+ + + + + +
+
+ `; + + const form = document.querySelector("form"); + const id1 = form.querySelector("#i1"); + const id2 = form.querySelector("#i2"); + const id3 = form.querySelector("#i3"); + const id31 = form.querySelector("#i31"); + const id4 = form.querySelector("#i4"); + const id5 = form.querySelector("#i5"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + id1.checked = true; + id1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(id1.matches(":invalid")).toBe(true); + expect(id2.matches(":invalid")).toBe(true); + expect(id3.matches(":invalid")).toBe(true); + expect(id4.matches(":invalid")).toBe(true); + expect(id5.matches(":invalid")).toBe(true); + expect(form.querySelectorAll("em.warning").length).toBe(1); + expect(form.querySelectorAll("em.warning")[0].textContent).toBe( + "Please select at least 2 options" + ); + + id2.checked = true; + id2.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(id1.matches(":invalid")).toBe(false); + expect(id2.matches(":invalid")).toBe(false); + expect(id3.matches(":invalid")).toBe(false); + expect(id4.matches(":invalid")).toBe(false); + expect(id5.matches(":invalid")).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(0); + + id31.selected = true; + id3.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(id1.matches(":invalid")).toBe(false); + expect(id2.matches(":invalid")).toBe(false); + expect(id3.matches(":invalid")).toBe(false); + expect(id4.matches(":invalid")).toBe(false); + expect(id5.matches(":invalid")).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(0); + + id4.value = "okay"; + id4.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(id1.matches(":invalid")).toBe(true); + expect(id2.matches(":invalid")).toBe(true); + expect(id3.matches(":invalid")).toBe(true); + expect(id4.matches(":invalid")).toBe(true); + expect(id5.matches(":invalid")).toBe(true); + expect(form.querySelectorAll("em.warning").length).toBe(1); + expect(form.querySelectorAll("em.warning")[0].textContent).toBe( + "Please select at most 3 options" + ); + + id2.checked = false; + id2.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(id1.matches(":invalid")).toBe(false); + expect(id2.matches(":invalid")).toBe(false); + expect(id3.matches(":invalid")).toBe(false); + expect(id4.matches(":invalid")).toBe(false); + expect(id5.matches(":invalid")).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(0); + + id5.value = "okay"; + id4.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(id1.matches(":invalid")).toBe(true); + expect(id2.matches(":invalid")).toBe(true); + expect(id3.matches(":invalid")).toBe(true); + expect(id4.matches(":invalid")).toBe(true); + expect(id5.matches(":invalid")).toBe(true); + expect(form.querySelectorAll("em.warning").length).toBe(1); + expect(form.querySelectorAll("em.warning")[0].textContent).toBe( + "Please select at most 3 options" + ); + + id4.value = ""; + id4.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(id1.matches(":invalid")).toBe(false); + expect(id2.matches(":invalid")).toBe(false); + expect(id3.matches(":invalid")).toBe(false); + expect(id4.matches(":invalid")).toBe(false); + expect(id5.matches(":invalid")).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(0); + }); + + it("9.3 - supports a value of 0 and replaces %{count}", async function () { + document.body.innerHTML = ` +
+
+ + +
+
+ `; + + const form = document.querySelector("form"); + const check1 = form.querySelector("[name=check][value='1']"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + check1.checked = true; + check1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(check1.matches(":invalid")).toBe(true); + expect(form.querySelectorAll("em.warning").length).toBe(1); + expect(form.querySelector("em.warning").textContent).toBe( + "You need to select at most 0 item(s)." + ); + + check1.checked = false; + check1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(check1.matches(":invalid")).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(0); + }); + + it("9.4 - validates a group of differently named inputs defined by a wrapper", async function () { + document.body.innerHTML = ` +
+
+ + + +
+ +
+ `; + + const form = document.querySelector("form"); + const fieldset = form.querySelector("fieldset"); + const c1 = form.querySelector("#c1"); + const c2 = form.querySelector("#c2"); + const c3 = form.querySelector("#c3"); + const other = form.querySelector("#other"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + // Nothing checked. + c1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(c1.matches(":invalid")).toBe(true); + expect(c2.matches(":invalid")).toBe(true); + expect(c3.matches(":invalid")).toBe(true); + // The input outside the group is not affected. + expect(other.matches(":invalid")).toBe(true); // required + expect(fieldset.querySelectorAll("em.warning").length).toBe(1); + expect(fieldset.querySelector("em.warning").textContent).toBe( + "Select at least one" + ); + // The error message is placed after the label of the last input. + expect( + fieldset.querySelector("label:nth-of-type(3) + em.warning") + ).toBeTruthy(); + + // One checked - valid. + c2.checked = true; + c2.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(c1.matches(":invalid")).toBe(false); + expect(c2.matches(":invalid")).toBe(false); + expect(c3.matches(":invalid")).toBe(false); + expect(fieldset.querySelectorAll("em.warning").length).toBe(0); + + // Two checked - valid. + c3.checked = true; + c3.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(c1.matches(":invalid")).toBe(false); + expect(fieldset.querySelectorAll("em.warning").length).toBe(0); + + // Three checked - invalid. + c1.checked = true; + c1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(c1.matches(":invalid")).toBe(true); + expect(c2.matches(":invalid")).toBe(true); + expect(c3.matches(":invalid")).toBe(true); + expect(fieldset.querySelectorAll("em.warning").length).toBe(1); + expect(fieldset.querySelector("em.warning").textContent).toBe( + "Select at most two" + ); + + // Back to two checked - valid. + c1.checked = false; + c1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(c1.matches(":invalid")).toBe(false); + expect(c2.matches(":invalid")).toBe(false); + expect(c3.matches(":invalid")).toBe(false); + expect(fieldset.querySelectorAll("em.warning").length).toBe(0); + }); + + it("9.5 - validates a wrapper group on submit", async function () { + document.body.innerHTML = ` +
+
+ + +
+ +
+ `; + + const form = document.querySelector("form"); + const c2 = form.querySelector("#c2"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + const submit_event = events.submit_event(); + form.dispatchEvent(submit_event); + await utils.timeout(1); // wait a tick for async to settle. + + expect(submit_event.defaultPrevented).toBe(true); + expect(form.querySelectorAll("em.warning").length).toBe(1); + expect(form.querySelector("#c2 + em.warning")).toBeTruthy(); + expect(form.querySelector("button").disabled).toBe(true); + + c2.checked = true; + c2.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(form.querySelectorAll("em.warning").length).toBe(0); + expect(form.querySelector("button").disabled).toBe(false); + }); + + it("9.6 - groups by name if the input itself defines min/max values", async function () { + document.body.innerHTML = ` +
+
+ + + +
+
+ `; + + const form = document.querySelector("form"); + const b1 = form.querySelector("#b1"); + const b2 = form.querySelector("#b2"); + const a1 = form.querySelector("#a1"); + + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + // The b group is checked by name: 1 of b checked - valid. + b1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(b1.matches(":invalid")).toBe(false); + expect(b2.matches(":invalid")).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(0); + + // The fieldset group: a1 and b1 checked - max-values 1 exceeded. + a1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(a1.matches(":invalid")).toBe(true); + expect(form.querySelectorAll("em.warning").length).toBe(1); + }); + }); + });