Replace dependencies with if-then-else structure

Created on 4 April 2024, 3 months ago
Updated 31 May 2024, 23 days ago

Problem/Motivation

The "checked" or "empty" triggers cannot be used currently. It's quite common to control visibility or required states of form elements with checkboxes, or make sure that some field is filled when another is empty, so this would be a nice feature improvement. Also having multiple triggers per state is quite a common requirement.

โœจ Feature request
Status

Needs work

Version

2.0

Component

Code

Created by

๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mErilainen
  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

    Here is a patch which will add support for the checked trigger. It works with both "visible" and "required" states.
    This approach alters the schema properties and uses the if & then properties instead of using the "dependencies" keyword which is not supported by the latest JSON Schema if I understood correctly.

  • Status changed to Needs review 3 months ago
  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

    Here is an improved version of the patch which also works with the "empty" trigger, at least to a certain degree.
    Having the patch from โœจ Default value not available in response Needs review helps in some cases.

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

    Yet slightly improved version which uses if-not-then properties instead of if-else. Seems to work better without default values in the schema too.

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

    This iteration of the patch adds support for multiple triggers per state and moves the handling of the "filled" trigger from the prepareDependencySchema() to the alterSchemaProperties() so that multiple "filled" triggers can be used per state.
    The webformElementStatesAfterBuild() has not been touched yet because I could not get it working on my local.

    It starts to feel like major refactoring could be in place, but there is still a lot of handling of the "Value is" trigger which I don't want to touch and might not need to be changed in the end.

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

    Fix for "Notice: Only variable references should be returned by reference".

  • Status changed to Needs work 2 months ago
  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

    This patch takes more drastic approach and removed the use of the non-draft dependencies keyword completely. "Values is" trigger is also handled in the if-then-else structure which seems to work nicely too.
    It also changes the getDependencyKey() function so that it will not throw an exception, because it seems to break Webform UI if there is a container element anywhere in a webform. This part needs still more work, because currently it allows to add any element into a condition and many of them do not work correctly.

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

    This version will replace most of the conditions from if-then to if-not-else because the if-then else logic seems to require some default values in the schema for the elements. The if-not-else seems to work better and makes validation easier when there are no default values in the elements.

  • Status changed to Needs review about 2 months ago
  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

    This will just affect the Drupal UI so that only "oneOf" operator can be selected. The if-then-else (with "not" in the mix) doesn't seem to work easily with "allOf" or anyOf" operators.
    Marking as "Needs review" because it would be nice to know if this approach makes sense.

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

    I had incorrect operator machine name, changed "one" to "xor". Also setting default value didn't work, so I removed that.

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland mErilainen

    And apparently that broke the rendering, so I will just fake the selection "All" as "One" which works with the if-then-else logic.

  • Status changed to Needs work 23 days ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States KurtTrowbridge

    I think this is related to this issue and its patch, but apologies if it's unrelated. I added this patch because I was having issues with a field that has multiple visibility conditions (e.g. Field 2 is visible if the answer to Field 1 was Option A or Option B). With the patch installed, that results in an error on line 64 ($trigger = key($triggerValue);) because of the or condition: the $triggers array that is looped through includes a non-array value, or, between the actual visibility conditions, so there's no array key to assign to the $trigger variable. I've attached a screenshot of what the $triggers variable contains. Not sure if it's as straightforward as confirming that $triggerValue is an array before continuingโ€”I may try it and report backโ€”or if that would cause an issue with the or condition not taking effect.

    (Possibly of note: I have the patch installed atop version 2.0.10, not 2.0.x-dev, since the dev version is behind it and doesn't support version 6 of Webform.)

Production build 0.69.0 2024