We are experiencing a conditional logic problem and I have attached the exported test form which demonstrates the problem.

There is a field test_other which should be hidden if the value in the field type is not "a" and not "b". This is working in 3 out of 4 cases:

  1. If type is empty, test_other is is visible: ok
  2. If type is "a", test_other is is hidden: ok
  3. If type is "b", test_other is is visible: NOT ok
  4. If type is something else, test_other is is visible: ok

Now, if you look at the source you notice that there is a string "null" in the states definition of test_other. This might be related and what's strange is that we have no way of getting rid of this extra data in the source. Not even when editing the exported yaml file and importing it again. Drupal recognizes that the config has changed but when importing it, the "null" is still there.

For further testing, the form contains another field test_jh which achieves the same logic, but the other way round: here the logic says that the field is INvisible when type is either "a" or "b".

With this second example, the logic works as expected and we don't have the problem with the "null" string in the source. I know this is a crazy example and it only exists to reproduce the problem. So don't worry, the site builder is still in shape ;-)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jurgenhaas created an issue. See original summary.

jrockowitz’s picture

@jurgenhaas Edge case examples are always helpful. I tend to let a few example build up in the queue and then see if there are any patterns that can be easily or not easily addressed. Thanks.

gneef’s picture

@jrockowitz Are you planing any updates on this issue?

jrockowitz’s picture

No. I am busy dealing with other issues. Any help is welcomed.

jrockowitz’s picture

Status: Active » Needs review
FileSize
610 bytes

For combine conditions on the same element I recommend using a pattern.

type:
  '#type': textfield
  '#title': type
  '#description': 'Enter a or b'
test_a:
  '#type': checkbox
  '#title': test_a
  '#states':
    visible:
      ':input[name="type"]':
        value: a
test_b:
  '#type': checkbox
  '#title': test_b
  '#states':
    visible:
      ':input[name="type"]':
        value: b
test_not_ab:
  '#type': checkbox
  '#title': test_not_ab
  '#states':
    visible:
      ':input[name="type"]':
        value:
          '!pattern': (a|b)
test_ab:
  '#type': checkbox
  '#title': test_ab
  '#states':
    visible:
      ':input[name="type"]':
        value:
          pattern: (b|a)

The attached patch fixes an issue with the !pattern not executing.

jrockowitz’s picture

I think the example is also running into #2908379: Not saving all the states for the container element

  • jrockowitz committed d16c0c7 on 8.x-5.x
    Issue #2944736 by jrockowitz, jurgenhaas: Conditional logic issue when 2...
jrockowitz’s picture

Status: Needs review » Closed (duplicate)

I committed the patch that fixes the pattern issue but I am marking this as a duplicate issue.