[#States API] Conditionally hidden required field gives error on submit

Created on 16 March 2022, over 3 years ago
Updated 8 April 2024, over 1 year ago

I have a webform that's basically set up like this:

select:
  '#type': select
  '#title': 'Select Type'
  '#options':
    A: A
    B: B
    C: C
  '#required': true
select_1:
  '#type': select
  '#title': 'Select 1'
  '#options':
    'Yes': 'Yes'
    'No': 'No'
  '#required': true
container:
  '#type': container
  '#states':
    visible:
      ':input[name="select_1"]':
        value: 'Yes'
  select_2:
    '#type': select
    '#title': 'Select 2'
    '#options':
      'Yes': 'Yes'
      'No': 'No'
    '#states':
      invisible:
        - ':input[name="select"]':
            value: A
        - or
        - ':input[name="select"]':
            value: C
    '#required': true
  container_01:
    '#type': container
    '#states':
      visible:
        ':input[name="select_2"]':
          value: 'Yes'
    text_2:
      '#type': textfield
      '#title': 'Text 2'
      '#required': true

There's a select box with a few types, a select box to open a container, a select box in that container that may be hidden depending on the type chosen in the first select box, and a container that opens if this inside select box is set to yes. The inside select box will be hidden if the type is A or C. All fields are required.

When I set my type to A and the first select box to Yes, it properly hides the inside select box and doesn't show the inside container.
But clicking submit does nothing and instead gives an error in console:

An invalid form control with name='select_2' is not focusable.

The requirement on the inside select (named select_2 here) should some how be disabled if its hidden so the form can submit properly.

🐛 Bug report
Status

Active

Version

6.2

Component

Code

Created by

🇺🇸United States j_s

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States jrockowitz Brooklyn, NY
  • 🇺🇸United States esod

    I'm able to to replicate this issue too. Select 'Recuse' in the attached form.

  • 🇺🇸United States esod

    I was hoping this wasn't just a webform problem, but it is a webform problem.

  • 🇮🇳India dev2.addweb

    @esod It works with Plain text format. I think it having problem with ckeditor.

    In first webform issue, I have resolved by updating select_2 states

    select_2:
        '#type': select
        '#title': 'Select 2'
        '#options':
          'Yes': 'Yes'
          'No': 'No'
        '#states':
          invisible:
            - ':input[name="select"]':
                value: A
            - or
            - ':input[name="select"]':
                value: C
          required:
            ':input[name="select"]':
              value: B
            ':input[name="select_1"]':
              value: 'Yes'
  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦
  • 🇺🇸United States esod

    @nilesh.addweb Yes, it works with plain text format or a textarea, which is also plain text. We'd like this to work using a CKEditor 5 text_format element.

  • 🇺🇸United States esod

    This form uses only textfield, radio elements and #states.

    Open the Console on your browser.
    Select 'I am the nominee.'
    Fill out the form.
    Click Submit.
    See the 'An invalid form control...' error.

    Open the Console on your browser.
    Select 'I am submitting on behalf of the nominee.'
    Fill out the form.
    Click Submit.
    The form submits.

  • Status changed to Postponed: needs info about 2 months ago
  • 🇺🇸United States jrockowitz Brooklyn, NY

    For #10, you have two required conditions (submitter_information: visible and submitter_middle_name: required) that are conflicting. Note that submitter_information: visible sets required conditions on the subelements.

    Your code

         submitter_information:
          '#type': fieldset
          '#title': 'Submitter Information'
          '#states':
            visible:
              ':input[name="nominee_select_one"]':
                value: submitter
          '#states_clear': false
          submitter_no_middle_name:
            '#type': checkbox
            '#title': 'No Middle Name'
            '#help_title': ' '
          flexbox_submitter_name:
            '#type': webform_flexbox
            submitter_first_name:
              '#type': textfield
              '#title': 'First Name'
              '#required': true
              '#flex': 3
            submitter_middle_name:
              '#type': textfield
              '#title': 'Middle Name'
              '#flex': 3
              '#states':
                required:
                  ':input[name="submitter_no_middle_name"]':
                    unchecked: true
                readonly:
                  ':input[name="submitter_no_middle_name"]':
                    checked: true
            submitter_last_name:
              '#type': textfield
              '#title': 'Last Name'
              '#required': true
              '#flex': 3
    

    To fix this, you need to make the below change, which ensures both conditions for requiring submitter_middle_name are defined as expected.

          submitter_middle_name:
            '#type': textfield
            '#title': 'Middle Name'
            '#flex': 3
            '#states':
              required:
                ':input[name="nominee_select_one"]':
                  value: submitter          
                ':input[name="submitter_no_middle_name"]':
                  unchecked: true
              readonly:
                ':input[name="submitter_no_middle_name"]':
                  checked: true
    
  • 🇺🇸United States jrockowitz Brooklyn, NY

    The original example seems to have the same nested condition issue as #10

  • 🇺🇸United States jrockowitz Brooklyn, NY

    I am realizing that this ticket has multiple conditional logic issues being mentioned, and it makes it very difficult to resolve any of them.

    The original issue can be solved by ensuring a container's conditions are applied to the subelements that have additional conditional logic.

    Please open new tickets for the other issues mentioned.

  • Now that this issue is closed, please review the contribution record.

    As a contributor, attribute any organization helped you, or if you volunteered your own time.

    Maintainers, please credit people who helped resolve this issue.

Production build 0.71.5 2024