update validation code

This commit is contained in:
Beverly Nguyen
2025-03-24 12:33:01 -07:00
parent 3a38b8bf31
commit 7e2a2bad8e
7 changed files with 19 additions and 25 deletions

View File

@@ -38,7 +38,7 @@ function attachValidation() {
const validatedRadioNames = new Set(); const validatedRadioNames = new Set();
inputs.forEach((input) => { inputs.forEach((input) => {
const errorId = input.id ? `${input.id}-error` : `${input.name}-error`; const errorId = input.type === "radio" ? `${input.name}-error` : `${input.id}-error`;
let errorElement = document.getElementById(errorId); let errorElement = document.getElementById(errorId);
if (!errorElement) { if (!errorElement) {
@@ -46,21 +46,20 @@ function attachValidation() {
errorElement.id = errorId; errorElement.id = errorId;
errorElement.classList.add("usa-error-message"); errorElement.classList.add("usa-error-message");
errorElement.setAttribute("aria-live", "polite"); errorElement.setAttribute("aria-live", "polite");
errorElement.style.display = "none";
if (input.type === "radio") { if (input.type === "radio") {
const group = form.querySelectorAll(`input[name="${input.name}"]`); const group = form.querySelectorAll(`input[name="${input.name}"]`);
const lastRadio = group[group.length - 1]; const lastRadio = group[group.length - 1];
lastRadio.parentElement.insertAdjacentElement("afterend", errorElement); lastRadio.parentElement.insertAdjacentElement("afterend", errorElement);
} else { } else {
input.insertAdjacentElement("afterend", errorElement); input.insertAdjacentElement("afterend", errorElement);
} } }
}
if (input.type === "radio") { if (input.type === "radio") {
if (validatedRadioNames.has(input.name)) { if (validatedRadioNames.has(input.name)) return;
return; // already validated this group
}
validatedRadioNames.add(input.name); validatedRadioNames.add(input.name);
// Find all radio buttons with the same name const radioGroup = form.querySelectorAll(`input[name="${input.name}"]`);
const radioGroup = document.querySelectorAll(`input[name="${input.name}"]`);
const isChecked = Array.from(radioGroup).some(radio => radio.checked); const isChecked = Array.from(radioGroup).some(radio => radio.checked);
if (!isChecked) { if (!isChecked) {
@@ -87,19 +86,18 @@ function attachValidation() {
inputs.forEach((input) => { inputs.forEach((input) => {
input.addEventListener("input", function () { input.addEventListener("input", function () {
const errorElement = document.getElementById(input.id ? `${input.id}-error` : `${input.name}-error`); const errorId = input.type === "radio" ? `${input.name}-error` : `${input.id}-error`;
const errorElement = document.getElementById(errorId);
if (errorElement && input.value.trim() !== "") { if (errorElement && input.value.trim() !== "") {
hideError(input, errorElement); hideError(input, errorElement);
} }
}); });
if (input.type === "radio") { if (input.type === "radio") {
input.addEventListener("change", function () { input.addEventListener("change", function () {
console.log(`Radio ${input.id} changed`); const errorElement = document.getElementById(`${input.name}-error`);
if (errorElement) {
const groupError = document.getElementById(`${input.name}-error`) hideError(input, errorElement);
|| document.getElementById(`${input.id}-error`); }
if (groupError) hideError(input, groupError);
}); });
} }
}); });

View File

@@ -781,9 +781,6 @@ def govuk_radios_field_widget(self, field, param_extensions=None, **kwargs):
# returns either a list or a hierarchy of lists # returns either a list or a hierarchy of lists
# depending on how get_items_from_options is implemented # depending on how get_items_from_options is implemented
items = self.get_items_from_options(field) items = self.get_items_from_options(field)
is_field_required = False
if getattr(field, 'flags', None) and 'required' in field.flags:
is_field_required = True
params = { params = {
"name": field.name, "name": field.name,
@@ -796,8 +793,6 @@ def govuk_radios_field_widget(self, field, param_extensions=None, **kwargs):
}, },
"errorMessage": error_message, "errorMessage": error_message,
"items": items, "items": items,
"required": is_field_required,
} }
# extend default params with any sent in during instantiation # extend default params with any sent in during instantiation

View File

@@ -56,6 +56,7 @@
{% set itemHintId = id + '-item-hint' %} {% set itemHintId = id + '-item-hint' %}
<div class="usa-radio"> <div class="usa-radio">
<input class="usa-radio__input" id="{{ id }}" name="{{ params.name }}" type="radio" value="{{ item.value }}" <input class="usa-radio__input" id="{{ id }}" name="{{ params.name }}" type="radio" value="{{ item.value }}"
{{-" checked" if item.checked }}
{{-" disabled" if item.disabled }} {{-" disabled" if item.disabled }}
{%- if item.conditional %} data-aria-controls="{{ conditionalId }}"{% endif -%} {%- if item.conditional %} data-aria-controls="{{ conditionalId }}"{% endif -%}
{%- if hasHint %} aria-describedby="{{ itemHintId }}"{% endif -%} {%- if hasHint %} aria-describedby="{{ itemHintId }}"{% endif -%}

View File

@@ -14,7 +14,7 @@
{% block maincolumn_content %} {% block maincolumn_content %}
{% call form_wrapper() %} {% call form_wrapper(data_force_focus=True) %}
{{ page_header('Free text message allowance') }} {{ page_header('Free text message allowance') }}
{{ form.free_sms_allowance }} {{ form.free_sms_allowance }}
{{ page_footer('Save') }} {{ page_footer('Save') }}

View File

@@ -14,7 +14,7 @@
{% block maincolumn_content %} {% block maincolumn_content %}
{% call form_wrapper() %} {% call form_wrapper(data_force_focus=True) %}
{{ page_header('Message batch limit') }} {{ page_header('Message batch limit') }}
{{ form.message_limit }} {{ form.message_limit }}
{{ page_footer('Save') }} {{ page_footer('Save') }}

View File

@@ -14,7 +14,7 @@
{% block maincolumn_content %} {% block maincolumn_content %}
{% call form_wrapper() %} {% call form_wrapper(data_force_focus=True) %}
{{ page_header('Rate limit') }} {{ page_header('Rate limit') }}
{{ form.rate_limit }} {{ form.rate_limit }}
{{ page_footer('Save') }} {{ page_footer('Save') }}

View File

@@ -15,7 +15,7 @@
<div class="grid-row"> <div class="grid-row">
<div class="grid-col-10"> <div class="grid-col-10">
{% call form_wrapper() %} {% call form_wrapper(data_force_focus=True) %}
{{ form.enabled(param_extensions={ {{ form.enabled(param_extensions={
"fieldset": { "fieldset": {
"legend": { "legend": {