diff --git a/modules/custom/wxt_ext/wxt_ext_webform/js/webform_checkboxes_group.js b/modules/custom/wxt_ext/wxt_ext_webform/js/webform_checkboxes_group.js new file mode 100644 index 00000000..248e7a49 --- /dev/null +++ b/modules/custom/wxt_ext/wxt_ext_webform/js/webform_checkboxes_group.js @@ -0,0 +1,209 @@ +/** + * Collapse WET-BOEW / jQuery Validate errors for Webform checkbox *groups* so: + * - "at least one checked" is enforced via require_from_group + * - only ONE inline error is rendered for the whole set + * - only ONE item appears in the WET error summary + * - checking ANY checkbox in the set clears the group error + * + * How it works (high level): + * 1) We wait until WET's validator is initialized on the form. + * 2) We discover checkbox groups by looking for inputs named like base[option] + * that also have data-rule-require_from_group (added in PHP preprocess). + * 3) We register jQuery Validate "groups" for each base so the set acts as one field. + * 4) We only *place* an inline error label for the FIRST checkbox of each base. + * (The others are skipped to avoid duplicate labels.) + * 5) We de-duplicate the validator's error list in invalidHandler so WET's top + * summary only contains one entry per base. + * 6) We prevent submission when the validator reports invalid, covering + * normal submit + Enter key + direct submit-button clicks. + */ + +(function ($, Drupal, once) { + Drupal.behaviors.wetGroupOneMessage = { + attach(context) { + // Bind this behavior once per element with .wb-frmvld. + const hosts = once('wetGroupOneMessage', '.wb-frmvld', context); + + hosts.forEach((host) => { + const $host = $(host); + + // Find the