Right now I can use conditionals to change the state of one field based on the value in another field, but what if I want to base that logic on a range of values? Unless I'm missing something, the current conditionals logic is extremely limited when it comes to evaluating anything except single, explicitly defined values.

For example: I want to show a Message element when someone selects a value in a Time element that falls into a particular time range (let's say 5:00 PM to 11:59 PM). To do that, I'd have to list every possible value that I want to target in the Time element through the UI which would output the following YAML source code:

message_hours_of_operation:
    '#type': message
    '#states':
      visible:
        - ':input[name="desired_time"]':
            value: '5:01 PM'
        - or
        - ':input[name="desired_time"]':
            value: '5:02 PM'
        - or
        - ':input[name="desired_time"]':
            value: '5:03 PM'
        - or
        - ':input[name="desired_time"]':
            value: '5:04 PM'
        - or
        - ':input[name="desired_time"]':
            value: '5:05 PM'

... and on it goes bloating the YAML source code.

I know we're just evaluating text strings here so adding something like "between" as a conditional operator isn't going to make sense. BUT I'd love to see regex matches for conditional logic just like Webform's validation currently uses. That way If i wanted to target 5:00 PM to 11:59 PM, I could use a regular expression like this:
^[5-9]{1}:\d\d [P][M]|^[1]{1}[0-1]{1}:\d\d [P][M]

What do you think?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nodecode created an issue. See original summary.

jrockowitz’s picture

I like the idea but I have no idea how to extend Drupal's States API to implement it.

jrockowitz’s picture

The solution included in this blog post still works for D8. I am just not sure how to implement it.

nodecode’s picture

Hmm. I wasn't aware this functionality was all performed by the states api. I was curious why the recent addition of the "readonly" property didn't show up as an option in the conditional logic dropdown.

Does this D7 implementation provide any clues? #1340616: Value as Regex Expression for comparing Furthermore, while the patch from that issue wasn't committed, the maintainer of the D7 Conditional Fields module says they did implement functionality to address that issue. I'm not sure if he means they implemented regex or something else.

nodecode’s picture

I can confirm Conditional Fields for D8 does include evaluation of regular expressions... and it works well.

  • jrockowitz committed b4a7356 on 2932117-regex-conditions
    Issue #2932117: Regular Expressions for Conditionals logic
    
jrockowitz’s picture

Status: Active » Needs review
FileSize
18.53 KB

Using the approach from this blog post I was able to implement (regex) pattern, less than, and greater than clientside and server-side support.

jrockowitz’s picture

BTW, I went with 'pattern' instead of 'regex' because HTML5 input validation uses the 'pattern' attribute which happens to be a regex.

Please review. I might just commit this patch tomorrow.

  • jrockowitz committed 45cd900 on 8.x-5.x
    Issue #2932117 by jrockowitz: Regular Expressions for Conditionals logic
    
jrockowitz’s picture

Status: Needs review » Fixed

I committed the patch. Please download the latest dev release to review.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

nodecode’s picture

It works for me as far as I can tell. This is an amazing feature. Thank you!