From 0896e4faafe7fda693abe4e11bb1eaf50b2ad509 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Sun, 29 Nov 2026 10:34:33 +0100 Subject: [PATCH 1/2] maint(docs): Add ADR 0008 on own and group errors in pat-validation. Assisted-by: Claude Opus 5.5 --- ...008-pat-validation-own-and-group-errors.md | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 docs/adr/0008-pat-validation-own-and-group-errors.md diff --git a/docs/adr/0008-pat-validation-own-and-group-errors.md b/docs/adr/0008-pat-validation-own-and-group-errors.md new file mode 100644 index 000000000..b1efc4e01 --- /dev/null +++ b/docs/adr/0008-pat-validation-own-and-group-errors.md @@ -0,0 +1,69 @@ +# 0008 pat-validation: Separate own errors and group errors + +Status: Accepted (2026-11-29) + +## Context + +pat-validation treated all inputs with the same name as one group which shared +a single validation state: + +- `set_error()` set the custom validity on all inputs with the same name. +- `check_input()` started by clearing the custom validity of all of them. +- `remove_error()` cleared the state and removed the error messages of all of + them. + +This was introduced for radio and checkbox groups and for `min-values` and +`max-values`, where the error belongs to the group. + +But each input only runs its own checks. Checking one input of a group cleared +the errors which another input of the group had set, and did not set them +again. Forms which use the same name for inputs which are not a group broke. +For example, a date input and a time input named `start`, which the server +reads as a `[date, time]` list: a `not-after` error on the date input was +cleared when the time input was checked, and an invalid form could be +submitted. Zope `:list` and `:records` forms have the same problem. + +Deciding which inputs form a group, e.g. by input type or by the presence of +`min-values`/`max-values`, does not solve this. As soon as a group mixes +checks of single inputs with checks of the group, the errors of the single +inputs are lost again. + +## Decision + +pat-validation distinguishes two kinds of errors: + +- **Own errors** belong to a single input: all native errors (except the + `required` error of a radio group), `not-after`, `not-before`, `equality` + and the custom messages for native errors. They are set, cleared and shown + only for that input. Checking an input never touches the own errors of + other inputs with the same name. +- **Group errors** belong to all inputs with the same name: `min-values`, + `max-values` and a required radio group. They only depend on the whole + group, so any input of the group computes the same result. Every check of + an input of the group computes the group error again and sets it on all + inputs of the group. `min-values` and `max-values` are read from the first + input of the group which defines them. + +The custom validity of an input is its own error or, if there is none, the +group error. + +An own error message is shown after its input (or its wrapping label). The +group error message is shown once, after the last input of the group. It is +not shown while an input of the group shows an own error. The user fixes +those first. + +## Consequences + +- Same-named inputs which are not a group, e.g. date and time inputs or Zope + `:list` fields, keep their own errors. +- The result does not depend on the order in which the inputs are checked. +- There is no list of input types which form a group. The group checks apply + to all inputs with the same name, as before. +- Checkbox groups with `required` on single checkboxes get one message per + invalid checkbox instead of one message per group. A required radio group + still gets one message. +- Breaking change for JavaScript users: `set_error()` and `remove_error()` + only affect the given input. `remove_error()` defaults to + `all_of_group = false`. The new methods `check_group()`, + `render_error_message()` and `render_group_error_message()` handle the + group errors and the messages. From 10214199d70c1ed95a77103da239994ca4a987c3 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Sun, 29 Nov 2026 10:34:34 +0100 Subject: [PATCH 2/2] fix(pat-validation): Do not share errors between same-named inputs. Only errors which belong to the group of inputs with the same name - min-values, max-values and a required radio group - are shared between these inputs. All other errors belong to the single input. Before, checking one input cleared the errors of all inputs with the same name. E.g. a date and a time input with the same name: checking the time input cleared the not-after error of the date input and an invalid form could be submitted. This was a regression in 9.12.0-alpha.1. The group error is computed the same way by any input of the group, also if min-values or max-values is defined on only one input. The group error message is shown once after the last input of the group and only if no input of the group shows its own error. Breaking change for JavaScript users: set_error() and remove_error() only affect the given input. remove_error() defaults to all_of_group = false. The new methods check_group(), render_error_message() and render_group_error_message() handle the group errors and messages. Ref: ADR 0008 Assisted-by: Claude Opus 5.5 --- src/pat/validation/documentation.md | 11 +- src/pat/validation/validation.js | 424 +++++++++++++++++--------- src/pat/validation/validation.test.js | 214 +++++++++++++ 3 files changed, 508 insertions(+), 141 deletions(-) diff --git a/src/pat/validation/documentation.md b/src/pat/validation/documentation.md index cde2aed9a..69d929a94 100644 --- a/src/pat/validation/documentation.md +++ b/src/pat/validation/documentation.md @@ -89,7 +89,13 @@ In addition both the input element and its label will get an `warning` class. ``` -Checkboxes and radio buttons are treated differently: The error message is always set after the last element of the inputs with the same name. If that input is wrapped in a label, the error message is set after the label. +Errors which belong to a group of inputs with the same name are treated differently: a required radio group and the `min-values` and `max-values` checks (see below). +The error message of the group is shown once, after the last input of the group. If that input is wrapped in a label, the error message is set after the label. +All inputs of the group are invalid. + +All other errors belong to the single input, also if other inputs have the same name. +For example, a date input and a time input with the same name keep their own errors. +If an input of a group has its own error, the group error message is shown only after the own error is fixed. ```html
@@ -129,6 +135,9 @@ This allows a hidden input with the same name to submit a default value if no ch
``` +It is enough to define `min-values` and `max-values` on one input of the group. +If multiple inputs of the group define them, the options of the first one are used. + ### Form elements outside the form diff --git a/src/pat/validation/validation.js b/src/pat/validation/validation.js index d63a8005a..dc08169c7 100644 --- a/src/pat/validation/validation.js +++ b/src/pat/validation/validation.js @@ -34,8 +34,15 @@ parser.addArgument("delay", 100); // Delay before validation is done to avoid va // Aliases parser.addAlias("message-integer", "message-number"); +// The own error of an input - the error message node and the custom error +// message. const KEY_ERROR_EL = "__patternslib__input__error__el"; const KEY_ERROR_MSG = "__patternslib__input__error__msg"; +// The error of the group of inputs with the same name - e.g. min-values, +// max-values or a required radio group. Shared between all inputs of the +// group. The message is ``null`` if the group is valid. +const KEY_GROUP_ERROR_EL = "__patternslib__input__group__error__el"; +const KEY_GROUP_ERROR_MSG = "__patternslib__input__group__error__msg"; class Pattern extends BasePattern { static name = "validation"; @@ -174,12 +181,19 @@ class Pattern extends BasePattern { return; } - // In any case, clear the custom validity first. - this.set_error({ input: input, msg: "", skip_event: true }); + // In any case, clear the own error of the input first. Also clear the + // group error from the validity state while checking, so that the + // checks below only see the native and the own errors. The group + // error is re-applied in ``check_group``. + // Do not touch the other inputs with the same name. They keep their + // own errors. + input[KEY_ERROR_MSG] = ""; + input.setCustomValidity(""); const validity_state = input.validity; if (event?.submitter?.hasAttribute("formnovalidate")) { // Do not check when a button with ``formnovalidate`` was used. + this.apply_validity(input); return; } @@ -311,73 +325,13 @@ class Pattern extends BasePattern { await this.check_input({ input: not_before_el, stop: true }); } } - - 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)) { - // Do not count inputs which the user cannot change - - // hidden and disabled inputs, e.g. a hidden input which - // submits a default value - and buttons. - if ( - _input.disabled || - ["hidden", "submit", "button", "reset", "image"].includes( - _input.type, - ) - ) { - continue; - } - - // 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. - if (validity_state.valueMissing && input_options.message.required) { + if (input.type === "radio" && validity_state.valueMissing) { + // A required radio group is a group error, handled in + // ``check_group``. + } else if (validity_state.valueMissing && input_options.message.required) { this.set_error({ input: input, msg: input_options.message.required }); } else if (validity_state.rangeUnderflow && input_options.message.min) { this.set_error({ @@ -427,36 +381,172 @@ class Pattern extends BasePattern { input_options.message.datetime ) { this.set_error({ input: input, msg: input_options.message.datetime }); - } else { - // Still an error, but without customized messages. - // Call `emit_update` separately - this.emit_update("invalid"); } } + // The own error of the input - a custom error or a native error, but + // not the native error of a required radio group, which is a group + // error. + // At this point the validity state does not include the group error. + const own_invalid = + !validity_state.valid && + (validity_state.customError || + !(input.type === "radio" && validity_state.valueMissing)); + + // Check the group of inputs with the same name and apply the group + // error to all of them. + this.check_group(input); + + if (!validity_state.valid && event?.type === "submit") { + // Do not submit in error case and prevent other handlers to take + // action. Do this before any ``await``, otherwise the event + // cannot be canceled anymore. + event.preventDefault(); + event.stopPropagation(); + event.stopImmediatePropagation(); + } + + // Show or remove the error messages. + if (own_invalid) { + await this.render_error_message(input); + } else { + this.remove_error_message(input); + } + await this.render_group_error_message(input); + if (validity_state.valid) { - // No error to handle. Remove eventual error and return. - this.remove_error({ input }); + this.enable_disableables(); + this.emit_update("valid"); return; } - if (event?.type === "submit") { - // Do not submit in error case and prevent other handlers to take action. - event.preventDefault(); - event.stopPropagation(); - event.stopImmediatePropagation(); + if (!input[KEY_ERROR_MSG]) { + // ``set_error`` did not already emit the event. + this.emit_update("invalid"); } - await this.set_error_message(input); + + await this.disable_disableables(); } - set_error({ - input, - msg, - attribute = null, - min = null, - max = null, - skip_event = false, - }) { + check_group(input) { + // Check the validation rules which apply to the group of inputs with + // the same name: ``min-values``, ``max-values`` and a required radio + // group. Any input of the group can do this check and gets the same + // result. + // Set the group error on all inputs of the group. + const siblings = this.siblings(input); + + let group_msg = null; // null: valid. + + // Required radio group. + // A radio group is invalid, if one radio is required and none is + // checked. + const radios = siblings.filter((_input) => _input.type === "radio"); + const required_radio = radios.find( + (_input) => _input.required && !_input.disabled, + ); + if (required_radio && !radios.some((_input) => _input.checked)) { + group_msg = + parser.parse(required_radio).message.required || + this.native_message(required_radio); + } + + // min-values and max-values. + // Use the options of the first input of the group which defines + // them, so that it does not matter which input of the group is + // checked. + const group_options = + group_msg === null && + [input, ...siblings] + .map((_input) => parser.parse(_input)) + .find( + (options) => + options.minValues !== null || options.maxValues !== null, + ); + if (group_options) { + const min_values = + group_options.minValues === null + ? null + : parseInt(group_options.minValues, 10); + const max_values = + group_options.maxValues === null + ? null + : parseInt(group_options.maxValues, 10); + + let number_values = 0; + for (const _input of siblings) { + // Do not count inputs which the user cannot change - + // hidden and disabled inputs, e.g. a hidden input which + // submits a default value - and buttons. + if ( + _input.disabled || + ["hidden", "submit", "button", "reset", "image"].includes( + _input.type, + ) + ) { + continue; + } + + // 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) { + group_msg = this.format_message({ + input, + msg: group_options.message["max-values"], + max: max_values, + }); + } + if (min_values !== null && number_values < min_values) { + group_msg = this.format_message({ + input, + msg: group_options.message["min-values"], + min: min_values, + }); + } + } + + for (const _input of siblings) { + _input[KEY_GROUP_ERROR_MSG] = group_msg; + this.apply_validity(_input); + } + } + + apply_validity(input) { + // Set the custom validity of the input from its own error or - if + // there is none - from the error of its group. + input.setCustomValidity( + input[KEY_ERROR_MSG] || input[KEY_GROUP_ERROR_MSG] || "", + ); + } + + native_message(input) { + // Get the browser's validation message of the input, without the + // custom error. + input.setCustomValidity(""); + const msg = input.validationMessage; + this.apply_validity(input); + return msg; + } + + format_message({ input, msg, attribute = null, min = null, max = null }) { // Replace some variables, as like validate.js if (attribute) { msg = msg.replace(/%{attribute}/g, attribute); @@ -468,15 +558,26 @@ class Pattern extends BasePattern { msg = msg.replace(/%{count}/g, max); } msg = msg.replace(/%{value}/g, JSON.stringify(input.value)); + return msg; + } + + set_error({ + input, + msg, + attribute = null, + min = null, + max = null, + skip_event = false, + }) { + // Set the own error of the input. The other inputs with the same + // name are not affected. + msg = this.format_message({ input, msg, attribute, min, max }); - // 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). input[KEY_ERROR_MSG] = msg; + this.apply_validity(input); if (!skip_event) { this.emit_update("invalid"); @@ -485,84 +586,127 @@ class Pattern extends BasePattern { remove_error({ input, - all_of_group = true, + all_of_group = false, clear_state = true, skip_event = false, }) { - // Remove error message and related references from input. + // Remove the own error message and state of the input. + // With ``all_of_group``, also remove the own errors of all inputs + // with the same name and the group error. - let inputs = [input]; - if (all_of_group) { - // Get all inputs with the same name - e.g. radio buttons, checkboxes. - inputs = this.siblings(input); - } + const inputs = all_of_group ? this.siblings(input) : [input]; for (const _input of inputs) { if (clear_state) { - // Clear the error state directly instead of via - // ``set_error``, which would get the siblings of each sibling - // again. - _input.setCustomValidity(""); _input[KEY_ERROR_MSG] = ""; - } - const error_node = _input[KEY_ERROR_EL]; - _input[KEY_ERROR_EL] = null; - error_node?.remove(); - } - - // disable selector - if (this.form.checkValidity()) { - for (const _input of this.disableables) { - if (_input.disabled) { - _input.removeAttribute("disabled"); - _input.classList.remove("disabled"); + if (all_of_group) { + _input[KEY_GROUP_ERROR_MSG] = null; } + this.apply_validity(_input); } + this.remove_error_message(_input); } + if (all_of_group) { + this.remove_group_error_message(input); + } + + this.enable_disableables(); if (!skip_event) { this.emit_update("valid"); } } - async set_error_message(input) { - // First, remove the old error message. - 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.siblings(input); - if (inputs.length > 1 && inputs.some((_input) => !!_input[KEY_ERROR_EL])) { - // error message for input group already set. - return; + remove_error_message(input) { + // Remove the own error message node of the input. + const error_node = input[KEY_ERROR_EL]; + input[KEY_ERROR_EL] = null; + error_node?.remove(); + } + + remove_group_error_message(input) { + // Remove the group error message node of the group of the input. + for (const _input of this.siblings(input)) { + const error_node = _input[KEY_GROUP_ERROR_EL]; + _input[KEY_GROUP_ERROR_EL] = null; + error_node?.remove(); } + } + async create_error_node(message) { // Create the validation error DOM node from the template. // Sanitize the validation message to keep malicious input from being // executed. Chrome includes the input value in it's browser validation // message. When placing that into the DOM, malicious input could get // executed within the web page context. const dompurify = (await import("dompurify")).default; - const validation_message = dompurify.sanitize( - input.validationMessage || input[KEY_ERROR_MSG], - ); + const validation_message = dompurify.sanitize(message); + return dom.create_from_string(this.error_template(validation_message)) + .firstChild; + } - const error_node = dom.create_from_string( - this.error_template(validation_message), - ).firstChild; + async render_error_message(input) { + // Show the own error message of the input after the input. If the + // input is wrapped in a label, put the error message after the label. + // Use the custom error message or the browser's validation message. + const message = input[KEY_ERROR_MSG] || input.validationMessage; + const error_node = await this.create_error_node(message); - // 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); + // Remove the old error message. + this.remove_error_message(input); + + (input.closest("label") || input).after(error_node); input[KEY_ERROR_EL] = error_node; + } + + async render_group_error_message(input) { + // Show the group error message once, after the last input of the + // group. Do not show it, if an input of the group shows its own + // error - fix these first. + const siblings = this.siblings(input); + const group_msg = input[KEY_GROUP_ERROR_MSG]; + const show = + group_msg !== null && + group_msg !== undefined && + !siblings.some((_input) => !!_input[KEY_ERROR_EL]); + + if (!show) { + this.remove_group_error_message(input); + return; + } + + const error_node = await this.create_error_node(group_msg); + + // Remove the old group error message. + this.remove_group_error_message(input); + + const last_input = siblings[siblings.length - 1]; + (last_input.closest("label") || last_input).after(error_node); + for (const _input of siblings) { + _input[KEY_GROUP_ERROR_EL] = error_node; + } + } + + async set_error_message(input) { + // Show the own error message of the input and disable the + // disableables. + await this.render_error_message(input); + await this.disable_disableables(); + } + + enable_disableables() { + // Enable the disableables, if the form is valid. + if (!this.form.checkValidity()) { + return; + } + for (const _input of this.disableables) { + if (_input.disabled) { + _input.removeAttribute("disabled"); + _input.classList.remove("disabled"); + } + } + } + async disable_disableables() { let did_disable = false; for (const _input of this.disableables) { // Disable for melements if they are not already disabled and which diff --git a/src/pat/validation/validation.test.js b/src/pat/validation/validation.test.js index cc55cd36d..5159a35b6 100644 --- a/src/pat/validation/validation.test.js +++ b/src/pat/validation/validation.test.js @@ -2515,4 +2515,218 @@ describe("pat-validation", function () { expect(instance.siblings(input)).toEqual([input]); }); }); + + describe("13 - same-named inputs keep their own errors", function () { + const date_time_form = (extra = "") => ` +
+ + + + + +
`; + + it("13.1 - keeps not-after/not-before errors of date inputs next to same-named time inputs", async function () { + document.body.innerHTML = date_time_form(); + const form = document.querySelector("form"); + 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.querySelector("#s").validity.valid).toBe(false); + expect(form.querySelector("#e").validity.valid).toBe(false); + expect(form.querySelector("#st").validity.valid).toBe(true); + expect(form.querySelector("#et").validity.valid).toBe(true); + const warnings = form.querySelectorAll("em.warning"); + expect(warnings.length).toBe(2); + expect(warnings[0].textContent).toBe("start after end"); + expect(warnings[0].previousElementSibling.id).toBe("s"); + expect(warnings[1].textContent).toBe("end before start"); + expect(warnings[1].previousElementSibling.id).toBe("e"); + expect(form.querySelector("button").disabled).toBe(true); + }); + + it("13.2 - a live event on the time input does not clear the error of the date input", async function () { + document.body.innerHTML = date_time_form(); + const form = document.querySelector("form"); + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + const inp_s = form.querySelector("#s"); + inp_s.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + expect(inp_s.validity.valid).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(2); + + const inp_st = form.querySelector("#st"); + inp_st.dispatchEvent(events.focusout_event()); + await utils.timeout(1); // wait a tick for async to settle. + inp_st.dispatchEvent(events.input_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(inp_s.validity.valid).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(2); + expect(form.querySelector("button").disabled).toBe(true); + }); + + it("13.3 - keeps the date errors when the inputs also have min-values", async function () { + document.body.innerHTML = date_time_form( + `data-pat-validation="min-values: 1"`, + ); + const form = document.querySelector("form"); + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + await instance.validate_all(); + await utils.timeout(1); // wait a tick for async to settle. + + expect(form.querySelector("#s").validity.valid).toBe(false); + expect(form.querySelector("#e").validity.valid).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(2); + + // The group error appears once the own errors are gone. + form.querySelector("#s").value = ""; + form.querySelector("#st").value = ""; + form.querySelector("#st").dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + form.querySelector("#s").dispatchEvent(events.change_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].textContent).toBe( + "You need to select at least 1 item(s).", + ); + expect(warnings[0].previousElementSibling.id).toBe("st"); + expect(form.querySelector("#e").validity.valid).toBe(true); + }); + + it("13.4 - keeps the required error of one of two same-named text inputs", async function () { + document.body.innerHTML = ` +
+ + + +
`; + const form = document.querySelector("form"); + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + await instance.validate_all(); + await utils.timeout(1); // wait a tick for async to settle. + + expect(form.querySelector("#a1").validity.valid).toBe(false); + expect(form.querySelector("#a2").validity.valid).toBe(true); + const warnings = form.querySelectorAll("em.warning"); + expect(warnings.length).toBe(1); + expect(warnings[0].previousElementSibling.id).toBe("a1"); + + // Checking the other input keeps the error. + form.querySelector("#a2").dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + expect(form.querySelector("#a1").validity.valid).toBe(false); + expect(form.querySelectorAll("em.warning").length).toBe(1); + }); + + it("13.5 - keeps the group error when min-values is set on only one input", async function () { + document.body.innerHTML = ` +
+ + + + +
`; + const form = document.querySelector("form"); + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + const t1 = form.querySelector("#t1"); + const t3 = form.querySelector("#t3"); + t1.value = "one"; + t1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + expect(form.querySelectorAll("em.warning").length).toBe(1); + + t3.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + for (const inp of form.querySelectorAll("[name=t]")) { + expect(inp.validity.valid).toBe(false); + } + const warnings = form.querySelectorAll("em.warning"); + expect(warnings.length).toBe(1); + expect(warnings[0].previousElementSibling.id).toBe("t3"); + + t3.value = "two"; + t3.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + for (const inp of form.querySelectorAll("[name=t]")) { + expect(inp.validity.valid).toBe(true); + } + expect(form.querySelectorAll("em.warning").length).toBe(0); + }); + + it("13.6 - keeps the own error of a checkbox in a min-values group", async function () { + document.body.innerHTML = ` +
+ +
+ + +
+ +
`; + const form = document.querySelector("form"); + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + const c1 = form.querySelector("#c1"); + const c2 = form.querySelector("#c2"); + c1.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + expect(c1.validity.valid).toBe(false); + + // Fulfill min-values. The equality error of c1 must stay. + c2.checked = true; + c2.dispatchEvent(events.change_event()); + await utils.timeout(1); // wait a tick for async to settle. + + expect(c1.validity.valid).toBe(false); + expect(c2.validity.valid).toBe(true); + const warnings = form.querySelectorAll("em.warning"); + expect(warnings.length).toBe(1); + expect(warnings[0].previousElementSibling.id).toBe("c1"); + }); + + it("13.7 - shows one message for a required radio group, also when checked via another member", async function () { + document.body.innerHTML = ` +
+ + + + +
`; + const form = document.querySelector("form"); + const instance = new Pattern(form); + await events.await_pattern_init(instance); + + await instance.validate_all(); + 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].previousElementSibling.id).toBe("r3"); + + form.querySelector("#r2").checked = true; + form.querySelector("#r2").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); + }); + }); });