Back

Drupal Forms User Guide

Logic

Elements and pages can both be hidden or displayed depending on a value entered in a preceding field.  

A field element can also be made optional, required or read-only depending on a previous field. These settings are found in the Conditions tab. 

For example, if a user answers ‘yes’ to the question, ‘do you have a car?’, the model and registration questions can be shown. They can be hidden at the element level or, if there are several, they can be placed together on a separate page which is hidden until required. 

When to use logic 

The aim of logic is to hide questions that are not necessary to a user, or show them if a specific set of circumstances require extra information. 

It helps to map out the form in a white board first. Try to order it broadly like this: 

  1. Questions that filter out anyone who is not eligible to continue, or for whom the form is not relevant
  2. Questions that apply to everyone remaining
  3. Questions that only apply to a subset of users 

Branching logic 

A form may need to include multiple routes for different types of user.  

For example, if a user answers ‘no’ to ‘do you have a car?’, then we want:  

  • the page of questions about the model and registration to be hidden
  • the page of questions about why they don't have a car to be displayed  

If they answer ‘yes,’ the opposite will be needed. 

We can set this up by adding opposing conditions in the conditions tab of each page: 

The page about the user’s car is Visible if All of the following is met: Do you own a car? value is Yes 

The page about why they don’t own a car is Visible if All of the following is met: Do you own a car? value is No