Skip to content

Commit

Permalink
Don't validate input[type="checkbox"]:disabled
Browse files Browse the repository at this point in the history
When validating with `CheckboxValidator`, don't include `:disabled`
checkboxes when determining group validity.
  • Loading branch information
seanpdoyle committed Mar 6, 2024
1 parent 963269a commit fb5e3be
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* Add support for opting-into Checkbox group `[required]` validation

*Sean Doyle*

* Ignore `[disabled]` fields while validating

*Sean Doyle*
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ Then, render a group of `<input type="checkbox">` elements as `[required]`:
</fieldset>
```

Disabled form controls won't be validated.

#### How it works

To work-around the quirks of built-in support, `ConstraintValidations` monitors
Expand Down
10 changes: 5 additions & 5 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.

8 changes: 4 additions & 4 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.

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

const defaultOptions = {
Expand Down Expand Up @@ -188,7 +188,3 @@ function getValidationMessage(input) {

return validationMessage || input.validationMessage
}

function isFieldElement(element) {
return !element.disabled && "validity" in element && element.willValidate
}
4 changes: 4 additions & 0 deletions app/javascript/constraint_validations/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function isFieldElement(element) {
return !element.disabled && "validity" in element && element.willValidate
}

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,4 +1,4 @@
import { readValidationMessages } from "../util"
import { isFieldElement, readValidationMessages } from "../util"

export default class {
ignoringMutations = false
Expand Down Expand Up @@ -33,7 +33,7 @@ export default class {
}

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

Expand Down
5 changes: 3 additions & 2 deletions test/dummy/app/views/forms/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@

<%= form.collection_check_boxes :multiple_required_checkbox, [
["1", "Multiple required checkbox #1"],
["2", "Multiple required checkbox #2"]
["2", "Multiple required checkbox #2"],
["3", "Multiple required checkbox #3", disabled: true]
], :first, :second do |builder| %>
<%= builder.check_box %>
<%= builder.check_box builder.object.third.to_h %>
<%= builder.label %>
<% end %>
</fieldset>
Expand Down
10 changes: 7 additions & 3 deletions test/system/validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,17 @@ class ValidationsTest < ApplicationSystemTestCase
end

test "observes connection of multiple [required] checkboxes" do
connected = proc do |input|
input.assert_matches_selector :element, required: false, "aria-required": "true"
end

visit new_form_path(hotwire_enabled: true, checkbox: true)
click_button "Skip Validations"

within_fieldset "Multiple [required] checkboxes" do
assert_unchecked_field "Multiple required checkbox", exact: false, valid: false, count: 2 do |input|
input.assert_matches_selector :element, required: false, "aria-required": "true"
end
assert_unchecked_field "Multiple required checkbox #1", valid: false, &connected
assert_unchecked_field "Multiple required checkbox #2", valid: false, &connected
assert_unchecked_field "Multiple required checkbox #3", disabled: true, valid: true, &connected
end
end

Expand Down

0 comments on commit fb5e3be

Please sign in to comment.