Repository navigation
Forms: Fix required field and form validation for single and multiple options blocks #43738
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
Merged
talldan
merged 1 commit into
update/migrate-form-fields-to-inner-blocks
from
fix/required-form-field-validation-for-options-blocks
Jun 4, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch on the bug.
🤔 Unfortunately, I think there's some deeper issues here.
accessible-formseems to expecteddata-requiredto be on the element that has thegrunion-checkbox-multiple-optionsin some places.Here's one way to reproduce an issue:
Expected: The multiple choice is required so it shows a validation warning
Actual: No validation message. It's not clear whether the form was submitted
So I can see here in
getMultipleChoiceFieldsit query selects allgrunion-checkbox-multiple-optionselements:jetpack/projects/packages/forms/src/contact-form/js/accessible-form.js
Lines 345 to 347 in fc2bd2b
And the resulting elements are used here to check whether the form is valid:
jetpack/projects/packages/forms/src/contact-form/js/accessible-form.js
Lines 143 to 149 in fc2bd2b
In other places, the element is a different one. Sometimes it'll the the element that has
[aria-invalid](seegetInvalidFields).In
getFormFieldsit does specifically select thefieldset, so that's the one this bug fixes.I guess we need to decide on a single approach to selecting the block on the frontend and stick with it 😄
On the balance, this PR does improve things, so I'll approve it—a form with multiple choice as the only required field is less likely to happen than the case this PR fixes, which might be quite widespread.
Some things to follow-up on though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can reproduce this.
I think the inputs need required maybe?
It looks like the loop
for ( const field of multipleChoiceFields ) {inisFormValidis looking for inner fields.So if I change the selector to:
and add
data-required='" . esc_attr( $required ) . "'to the input inContact_Form_Field::render_checkbox_multiple_fieldit works?I haven't tested other scenarios though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think when you have required on an individual checkbox, it means that individual checkbox has to be checked. So if it were specified on all checkboxes, all checkboxes would need to be checked.
It works a little differently to required on a radio input.
From what I could tell, there's no way in HTML to declare that only one checkbox in a fieldset is required, so that's why forms is using something custom with a
data-requiredattribute.Here's a discussion about it - whatwg/html#6868.