Problem

I have a page with two Webforms, Form A for contacting the admin and Form B for ordering something. In both of them I have some conditional logic. In the second form (Form B) I want to add conditional logic to the phone-field to hide it until the firstname-field is filled (just a simplified example). The firstname-field exists in Form A and B.
The problem is now, that the logic for Form B works with the fields of Form A. When I add a firstname in Form B, phone-field of Form B stays hidden. When I add a firstname in Form A, phone-field of Form B will be visible.

Proposed resolution

I do not know exactly the code of the webform-module. But I think the conditional logic on a single page does not distinguish between multiple webforms on the same page.

The simplified code of the two forms.

Form A

firstname:
  '#type': textfield
  '#title': Firstname
  '#required': true
lastname:
  '#type': textfield
  '#title': Lastname
  '#required': true
contactmeby:
  '#type': radios
  '#title': 'Contact me by'
  '#options':
    Phone: Phone
    E-Mail: E-Mail
  '#required': true
phone:
  '#type': tel
  '#title': Phone
  '#states':
    visible:
      ':input[name="contactmeby"]':
        value: Phone
    required:
      ':input[name="contactmeby"]':
        value: Phone
  '#international': true
  '#international_initial_country': CH
e_mail:
  '#type': email
  '#title': E-Mail
  '#states':
    visible:
      ':input[name="contactmeby"]':
        value: E-Mail
    required:
      ':input[name="contactmeby"]':
        value: E-Mail

Form B

firstname:
  '#type': textfield
  '#title': Firstname
  '#required': true
lastname:
  '#type': textfield
  '#title': Lastname
  '#required': true
phone:
  '#type': tel
  '#title': Phone
  '#states':
    visible:
      ':input[name="firstname"]':
        filled: true
  '#international': true
  '#international_initial_country': CH
orders:
  '#type': own_order_element
  '#title': Orders

Comments

btemperli created an issue. See original summary.

jrockowitz’s picture

Status: Active » Closed (won't fix)

This seems like an edge case so I don't want fix this issue in the core webform module but here is the recipe to fix the problem.

If other people are having this problem and feel that this should be fixed in core webform module, please reopen this ticket.

BTW, writing the patch should be easy, fixing the broken tests is going to be most of work.

btemperli’s picture

Thank you for the recipe, jrockowitz!

tuh’s picture

Status: Closed (won't fix) » Active

I have the same problem. I need to have the same form more than once on the same page. It is a "did this help you?"-form with conditional with input fields and a send button.

Will the recipe also fix this issue ?

jrockowitz’s picture

@tuh Welcome to the Drupal Community

I wanted the say "Hello" and encourage you to learn more about...

Watch video about helping us help you

jrockowitz’s picture

The recipe should fix the same form more than once on the same page.

jrockowitz’s picture

Status: Active » Closed (works as designed)
amjad1233’s picture

Status: Closed (works as designed) » Active

I got the same issue. The use case was similar we have two type of audiences international student & domestic student in our site https://future-students.uq.edu.au. We hide and show forms based on the selection, we used the same form on the same page and Ajax stopped working also there were no errors in browser console.

cilefen’s picture

Status: Active » Closed (works as designed)

Please reopen if the recipe doesn’t work. You did not specify.

amjad1233’s picture

Status: Closed (works as designed) » Needs work

Yes the recipe worked well. But I think it would be good to add it as a patch.

cilefen’s picture

Status: Needs work » Closed (works as designed)

Mark this needs review when someone provides a patch.