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?
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | 2932117-regex-conditions-7.patch | 18.53 KB | jrockowitz |
Comments
Comment #2
jrockowitz commentedI like the idea but I have no idea how to extend Drupal's States API to implement it.
Comment #3
jrockowitz commentedThe solution included in this blog post still works for D8. I am just not sure how to implement it.
Comment #4
nodecode commentedHmm. 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.
Comment #5
nodecode commentedI can confirm Conditional Fields for D8 does include evaluation of regular expressions... and it works well.
Comment #7
jrockowitz commentedUsing the approach from this blog post I was able to implement (regex) pattern, less than, and greater than clientside and server-side support.
Comment #8
jrockowitz commentedBTW, 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.
Comment #10
jrockowitz commentedI committed the patch. Please download the latest dev release to review.
Comment #12
nodecode commentedIt works for me as far as I can tell. This is an amazing feature. Thank you!