This page describes using conditionals in Webform 4.
This page is still incomplete and requires more information.
Conditionals were brought into Webform core starting version 4 (7.x-4.x).
Component - one of current webform's fields.
Conditional - single set of rules, rule collation type and targets.
Rule - single set of comparison source, value and type. Source is a component, value is a component's value and type is a comparison type (equal, not equal etc.).
Rule collation type - AND or OR. Defines how results from multiple rules conditions will be collated. AND - all rules must pass the condition, OR - only one rule must pass the condition.
Target-component which will be shown or hidden based on condition.
Creating a condition
- Before you begin, you need to create all the components involved in condition logic (both source and target components).
- Take a look at tabs above. In Webform page of the node, between Form components and E-mails, you will find a tab Conditionals. Hit it. You should end up in
If you cannot find the tab, make sure you are using Webform 7.x-4.x. If you updated from 7.x-3.x, try clearing caches.
- Click on + button to create condition. Choose the source (triggering component of condition), comparison type and value to create a condition. As a reaction, choose the field which will be shown or hidden.
Really simple example:
if -Choose your country- -is- -USA-
then -show- -Select your state-
Giving a components structure (conditional, not hierarchical)
conditionals rules need to be created for each combination of all parent values as rules to both show and hide components in hierarchy.
This allows to provide proper parent-child-grandchild cascading behavior.
I.e. for field Field111 there are 2 conditionals to be created:
1. Field 1 == 11 AND Field11 == 111 => SHOW Field111
2. Field 1 != 11 OR Field11 != 111 => HIDE Field111
NOTE: AND is used for all SHOW, and OR - for all HIDE rules.
Each time a component is hidden, it's value is reset. This works for cascaded hide as well (provided that HIDE rule exists).
Examples and best practice
Here is a list of examples on how to use conditionals.
This is a work in progress, please feel free to contribute.
Show a component dependent on the url
If you are using a webform on multiple nodes (e.g. in panels or in a block) you can use a conditional to show or hide specific components dependent on the current url. And here is how:
- create a hidden / private text component that will not be visible for the enduser
- give this component a default value by using a url token; e.g. the [current-page:url:args:last] token will get the last part of the url (e.g. /europe/germany/[cologne])
- Now this hidden component can be used on the conditionals tab
if [hidden component] contains "cologne" then show [another field]
This will save you from creating multiple webforms which only differs in url specific components.