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
Comment #2
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThis 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.
Comment #3
btemperli CreditAttribution: btemperli at Gridonic commentedThank you for the recipe, jrockowitz!
Comment #4
tuh CreditAttribution: tuh commentedI 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 ?
Comment #5
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@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
Comment #6
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe recipe should fix the same form more than once on the same page.
Comment #7
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #8
amjad1233I 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.
Comment #9
cilefen CreditAttribution: cilefen commentedPlease reopen if the recipe doesn’t work. You did not specify.
Comment #10
amjad1233Yes the recipe worked well. But I think it would be good to add it as a patch.
Comment #11
cilefen CreditAttribution: cilefen commentedMark this needs review when someone provides a patch.