I have a Phone Number field, phone_1, and a Phone Number Type radio selection (Home, Cell, Work), phone_1_type. The phone_1_type should be required if phone_1 is filled, but the current code is making the fieldset that contains phone_1_type buttons required, and is allowing submission of the form without ever selecting a radio option.

Example YAML

phone_1_container:
    '#type': flexbox
    phone_1:
      '#type': tel
      '#title': 'Phone 1'
      '#description': 'Just enter the 10 digits of your number without spaces or dashes.'
      '#title_display': inline
      '#description_display': tooltip
      '#maxlength': '10'
      '#required': true
      '#pattern': '[0-9]{10}'
    phone_1_type:
      '#type': radios
      '#title': 'Phone 1 Type'
      '#title_display': invisible
      '#options':
        home: Home
        cell: Cell
        work: Work
      '#options_display': side_by_side
      '#states':
        required:
          ':input[name="phone_1"]':
            filled: true

Screenshot of HTML after filling in phone_1
Conditional Logic - Requiring Radios in a Fieldset

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

squidgle created an issue. See original summary.

jrockowitz’s picture

This is an issue with how Drupal core has implemented radios and checkboxes.

@see #994360: #states cannot check/uncheck checkboxes elements and #2836364: Wrapper attributes are not supported by composite elements, this includes radios, checkboxes, and buttons.

This is such a common element setup, I am tempted to create a new composite element called 'Telephone with type' (webform_phone).

ptsimard’s picture

This is such a common element setup, I am tempted to create a new composite element called 'Telephone with type' (webform_phone).

I'm actually in the process of researching through your code how to create composite elements.

Due to some requirements for a project I need a multiple values composite phone element that have 4 fields such as:

  • Phone type
  • Phone country code
  • Phone number
  • Phone extension

A similar thing will also be needed for :

  • email_type
  • email

So while my requirement is more specific and will require bespoke composite fields, I just wanted to add a "Me too" on the "Telephone with type" idea.

jrockowitz’s picture

@ptsimard I think this element is going to happen.

One major limitation is that composite elements do not support nested composite elements or nested multiple values.

For example, the contact composite can not include the telephone composite.

jrockowitz’s picture

Status: Active » Needs review

Please test the patch in #2856795: If radio buttons are required but not filled form is nevertheless submitted which also fixes this issue.

I have to give credit to @johnnny83 for inspiring the JavaScript workaround.

jrockowitz’s picture

Status: Needs review » Fixed

I committed the patch. Please download and review the latest dev release.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.