Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate Checkbox Groups on invalid events from other controls #34

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions app/assets/javascripts/constraint_validations.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/assets/javascripts/constraint_validations.es.js.map

Large diffs are not rendered by default.

44 changes: 35 additions & 9 deletions app/assets/javascripts/constraint_validations.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/assets/javascripts/constraint_validations.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/javascript/constraint_validations/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isFieldElement, readValidationMessages } from "./util"
import { isAriaInvalid, isFieldElement, readValidationMessages } from "./util"
import CheckboxValidator from "./validators/checkbox_validator"

const defaultOptions = {
Expand Down Expand Up @@ -83,7 +83,7 @@ export default class ConstraintValidations {

for (const form of forms) {
for (const element of Array.from(form.elements).filter(isFieldElement)) {
const serverRenderedInvalid = /true/i.test(element.getAttribute("aria-invalid"))
const serverRenderedInvalid = isAriaInvalid(element)
const id = element.getAttribute("aria-errormessage")
const errorMessageElement = document.getElementById(id)
const validationMessage = errorMessageElement?.textContent
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/constraint_validations/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export function isFieldElement(element) {
return !element.disabled && "validity" in element && element.willValidate
}

export function isAriaInvalid(element) {
return element.getAttribute("aria-invalid") === "true"
}

export function readValidationMessages(input) {
try {
return JSON.parse(input.getAttribute("data-validation-messages")) || {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isFieldElement, readValidationMessages } from "../util"
import { isAriaInvalid, isFieldElement, readValidationMessages } from "../util"

export default class {
selector = "input[type=checkbox]:required"
selector = "input[type=checkbox]"
ignoringMutations = false

constructor(constraintValidations, predicate) {
Expand All @@ -13,15 +13,17 @@ export default class {
}

connect() {
this.element.addEventListener("invalid", this.handleInvalid, { capture: true, passive: true })
this.mutationObserver.observe(this.element, {
attributeFilter: ["required"],
childList: true,
subtree: true
})
this.reportValidationMessages(this.element.querySelectorAll(this.selector))
this.reportValidationMessages(this.element.querySelectorAll(this.selector), isAriaInvalid)
}

disconnect() {
this.element.removeEventListener("invalid", this.handleInvalid, { capture: true, passive: true })
this.mutationObserver.disconnect()
}

Expand All @@ -30,8 +32,8 @@ export default class {
}

validate(target) {
const checkboxesInGroup = checkboxGroup(target).filter(isFieldElement)
const allRequired = checkboxesInGroup.every((checkbox) => checkbox.getAttribute("aria-required") === "true")
const checkboxesInGroup = checkboxGroup(target).filter(isCheckboxElement)
const allRequired = checkboxesInGroup.every((checkbox) => isRequired(checkbox))
const someChecked = checkboxesInGroup.some((checkbox) => checkbox.checked)

if (allRequired && someChecked) {
Expand All @@ -50,6 +52,18 @@ export default class {

// Private

handleInvalid = ({ target }) => {
const checkboxes = new Set

for (const element of target.form.elements) {
if (isCheckboxElement(element) && this.willValidate(element)) {
checkboxes.add(element)
}
}

this.reportValidationMessages(checkboxes)
}

handleMutation = (mutationRecords) => {
if (this.ignoringMutations) return

Expand All @@ -61,22 +75,23 @@ export default class {
target.removeAttribute("aria-required")
}
} else if (addedNodes.length) {
this.reportValidationMessages(addedNodes)
this.reportValidationMessages(addedNodes, isAriaInvalid)
}
}
}

reportValidationMessages(nodes) {
reportValidationMessages(nodes, willReport = () => true) {
const requiredCheckboxes = querySelectorAllNodes(this.selector, nodes)

for (const checkbox of requiredCheckboxes) {
if (checkbox.required) {
if (isRequired(checkbox)) {
const group = checkboxGroup(checkbox)

if (this.willValidateGroup(group)) {
for (const checkboxInGroup of group) {
this.swapRequiredWithAriaRequired(checkboxInGroup)
if (checkboxInGroup.getAttribute("aria-invalid") === "true") {

if (willReport(checkboxInGroup)) {
this.validate(checkboxInGroup)
}
}
Expand Down Expand Up @@ -138,3 +153,11 @@ function querySelectorAllNodes(selector, nodes, elements = new Set) {

return Array.from(elements)
}

function isCheckboxElement(element) {
return isFieldElement(element) && element.type === "checkbox"
}

function isRequired(element) {
return element.required || element.getAttribute("aria-required") === "true"
}
1 change: 1 addition & 0 deletions test/dummy/app/views/forms/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@
<% end %>
</fieldset>

<button>Validate and Submit</button>
<button formnovalidate>Skip Validations</button>
<% end %>
Loading
Loading