Skip to content

Conditional fields in CP - contains_any bugs #5293

@jameswtc

Description

@jameswtc

Bug description

When using conditional field in the CP, the regular expression incorrectly validates the contains_any

Source location:

/resources/js/components/field-conditions/Validators.js on Line 220 - 227

    passesIncludesAnyCondition(condition) {
        let values = condition.rhs.split(',').map(string => string.trim());

        if (Array.isArray(condition.lhs)) {
            return _.intersection(condition.lhs, values).length;
        }

        return new RegExp(values.join('|')).test(condition.lhs);
    }

The problem is with the Regular Expression, it matches any substrings of the conditions.

image

A solution would be to match only the whole string, e.g., with ^1_image|image_left_text_with_button_right$. This solutions assumes that the contains_any (which in the code is mapped to 'includes_any') is similar to the JS Array.includes(). Anyone attempts to match the substring will eventually fails.

Another, my preferred solution, is to introduce the proper "includes_any" (documented on the website but somehow no option in the drop-down), which behave just like the JavaScripts Array.includes(). This should be a non-breaking change for existing usage.

How to reproduce

Using conditional Field in the Fieldsets.

Have a Select dropdown field with the following options, let's say with handle layout_type:

  • 1_image,
  • text_left_1_image_right,
  • image_left_text_with_button_right

Create another field which show only on with the following conditions:

layout_type contains_any 1_image,image_left_text_with_button_right

Expected result: false
Actual result: true

Logs

No response

Versions

Statamic 3.2.33 Pro
Laravel 7.30.6
PHP 7.4.27
aryehraber/statamic-captcha 1.7.0
doublethreedigital/duplicator 1.3.4
withcandour/aardvark-seo 2.0.21

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions