Using Pattern Trigger (Regex) in Webform Conditional Logic in Drupal 8

✅ Quick Answer: Use ^(Value1|Value2|Value3)$ in Pattern trigger to check for multiple values.

When you need to create survey style forms in Drupal 8 Webform is the clear winner. It’s powerful enough to create all sorts of forms and you can even give it to your editor so they can create their own, after a little training, of course.

One part of Webform which I like is the ability to define conditional logic. For example, you can show or hide a text field based off a value from another element. You can also make an element conditionally required. It’s a very useful part of Webform, and you do all of this through a UI, no custom code.

Complex Conditional Logic

Defining simple conditional logic, check if element value has a single value, is pretty straightforward. But when you have to deal with multiple values, this is where things get tricky.

States can only be defined once, if you try and add another you’ll get an error such as:

If you were to add another property targeting the same element you’ll get this error:

Luckily, the error above tells us exactly what we should do “To use multiple values within a trigger try using the pattern trigger. ”

In this tutorial, you’ll learn how to use the Pattern trigger to check if an element has multiple values.

Getting Started

For this tutorial, I’m going to assume you know how to use Webform and that you’ve already created a form.

But if this is the first time you’ve heard of the module and want to learn more then check out these two tutorials:

  1. Getting Started with Webform in Drupal 8
  2. Moving Forward with Webform in Drupal 8

Check for Multiple Values using Pattern (Regex) Trigger

To demonstrate how this works imagine we have three elements: Type (select list) and then two text field elements.

If you select Drupal from the drop-down, a “Drupal version” text field appears. If you select WordPress, then a “WordPress version” appears. So far so good.

But then if you select “Drupal & WordPress” from the drop-down, then both elements should be visible. To achieve this you need to use the Pattern trigger instead of “Value is”.

When you define your visible property select Pattern and add ^(Value1|Value2|Value3)$ , this example ^(Drupal & WordPress|Drupal)$.

Summary

I understand this is an advanced topic. I’m no regex expert and I always end up spending a lot of time figuring out the correct expression. I recommend that you use https://regex101.com/ for testing the expression.

If you need to check if an element has multiple values, then using a Pattern trigger is the best way.

Have you done any cool conditional logic in Webform, if so leave a comment and let us know?

Ivan Zugec

About Ivan Zugec

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 10 years and has successfully completed several large Drupal projects in Australia.

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.