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?

About The Author

8 thoughts on “Using Pattern Trigger (Regex) in Webform Conditional Logic in Drupal 8”

  1. Hi Ivan,
    Great video.
    One more question: is it possible to use Drupal tokens inside a RegEx?
    If so, how to please? Any idea?

  2. Hi Ivan,
    Thanks for your reply. I think you’re right and that’s a pity.
    Maybe I should open a feature request about it.
    Don’t know if “Value is” condition accepts logic operators… will keep on investing.
    Cheers,
    -Gilbert

  3. Jonathan O'Harrow

    Hello! I’m attempting to use the conditional function in webforms to cut out some spam that we’re getting. Lately I’ve been getting spam messages all from this domain: @mail-free-mailer.online.

    Can I use the pattern function to block anything with that domain, regardless of the first part of the email address?

  4. Hi Ivan,
    Love your tutorials. Keep up the good work. Learned so much from you.

    Are you aware of any tutorials that actually explain the differences between the if All, if Any and if One selection?
    if All : then all conditions need to be met. No issues here.
    if Any vs if One : aren’t these the same?

    Thanks
    Stephane

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top