Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1248,13 +1248,13 @@ public function render_checkbox_multiple_field( $id, $label, $value, $class, $re
*/
$field = "<fieldset {$fieldset_id} class='grunion-checkbox-multiple-options " . $options_classes . "' style='" . $options_styles . "' " . ( $required ? 'data-required' : '' ) . '>';
} else {
$field = "<fieldset {$fieldset_id} class='jetpack-field-multiple__fieldset'>";
$field = "<fieldset {$fieldset_id} class='jetpack-field-multiple__fieldset'" . ( $required ? 'data-required' : '' ) . '>';
}

$field .= $this->render_legend_as_label( '', $id, $label, $required, $required_field_text );

if ( ! $is_outlined_style ) {
$field .= "<div class='grunion-checkbox-multiple-options " . $options_classes . "' style='" . $options_styles . "' " . ( $required ? 'data-required' : '' ) . '>';
$field .= "<div class='grunion-checkbox-multiple-options " . $options_classes . "' style='" . $options_styles . "' " . '>';

@talldan talldan Jun 3, 2025 •

Copy link
Copy Markdown
Contributor

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-form seems to expected data-required to be on the element that has the grunion-checkbox-multiple-options in some places.

Here's one way to reproduce an issue:

  1. Create a form that has a required Multiple Choice field and one other non-required field (of any other type)
  2. Save and view the frontend
  3. Leave Multiple Choice empty, but fill in the other field
  4. Submit

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 getMultipleChoiceFields it query selects all grunion-checkbox-multiple-options elements:

const getMultipleChoiceFields = form => {
return Array.from( form.querySelectorAll( '.grunion-checkbox-multiple-options' ) );
};

And the resulting elements are used here to check whether the form is valid:

const multipleChoiceFields = getMultipleChoiceFields( form );
for ( const field of multipleChoiceFields ) {
if ( isMultipleChoiceFieldRequired( field ) && ! isMultipleChoiceFieldValid( field ) ) {
return false;
}
}

In other places, the element is a different one. Sometimes it'll the the element that has [aria-invalid] (see getInvalidFields).

In getFormFields it does specifically select the fieldset, 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.

Copy link
Copy Markdown
Member

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 ) { in isFormValid is looking for inner fields.

So if I change the selector to:

	return Array.from(
		form.querySelectorAll( '.grunion-checkbox-multiple-options input[type="checkbox"]' )
	);

and add data-required='" . esc_attr( $required ) . "' to the input in Contact_Form_Field::render_checkbox_multiple_field it works?

I haven't tested other scenarios though.

Screenshot 2025-06-04 at 11 46 04 am

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the inputs need required maybe?

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-required attribute.

Here's a discussion about it - whatwg/html#6868.

}

$options_data = $this->get_attribute( 'optionsdata' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ const isFormSubmitting = form => {
const isMultipleChoiceField = elt => {
return (
elt.tagName.toLowerCase() === 'fieldset' &&
elt.classList.contains( 'grunion-checkbox-multiple-options' )
( elt.classList.contains( 'grunion-checkbox-multiple-options' ) ||
elt.querySelector( '.grunion-checkbox-multiple-options' ) !== null )
);
};

Expand All @@ -197,7 +198,9 @@ const isMultipleChoiceField = elt => {
*/
const isSingleChoiceField = elt => {
return (
elt.tagName.toLowerCase() === 'fieldset' && elt.classList.contains( 'grunion-radio-options' )
elt.tagName.toLowerCase() === 'fieldset' &&
( elt.classList.contains( 'grunion-radio-options' ) ||
elt.querySelector( '.grunion-radio-options' ) !== null )
);
};

Expand Down