Webform conditional logic doesn't work when the form is embedded

Created on 19 July 2023, over 1 year ago
Updated 27 June 2024, 6 months ago

Problem/Motivation

Hi!

I think there's a bug in webform where the conditional logic of webform elements does not work when embedding a multi-page webform within a render array using #type => 'webform', etc.

The webform's conditional logic does work when I view the webform in webform's actual "View" and "Test" pages.

Furthermore, when embedding a webform on a page using a block, the conditional logic also works (but the page refreshes between the page).

On a side note, when embedding the multi-page webform in a form (using the code below), the page isn't refreshed.

Steps to reproduce

Here's a small sample webform yaml configuration:

...
elements: |-
  page_1:
    '#type': webform_wizard_page
    '#title': 'Page 1'
    show_textbox:
      '#type': checkbox
      '#title': 'Show textbox'
    textbox:
      '#type': textarea
      '#title': Textbox
      '#states':
        visible:
          ':input[name="show_textbox"]':
            checked: true
  page_2:
    '#type': webform_wizard_page
    '#title': 'Page 2'
    show_textbox2:
      '#type': checkbox
      '#title': 'Show textbox'
    textbox2:
      '#type': textarea
      '#title': Textbox
      '#states':
        visible:
          ':input[name="show_textbox2"]':
            checked: true
...

Also, here's a sample Drupal Form class for embedding the webform:

<?php

namespace Drupal\test_webform_conditionals\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class TestForm extends FormBase
{

  public function getFormId()
  {
    return 'test_webform_conditionals';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    $form['#tree'] = TRUE;
    $form['webform'] = [
    '#type' => 'webform',
    '#webform' => 'test_conditionals', // webform id goes here
    '#lazy' => TRUE // If this is false, conditionals won't work at all. When it's true, at least the conditionals only work for the elements in the first page of the webform. But the conditional logic for the elements on the second and further pages won't work
    ];
    return $form;
  }

  public function validateForm(array &$form, FormStateInterface $form_state)
  {
  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
  }
}

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Closed: won't fix

Version

6.2

Component

Code

Created by

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

Comments & Activities

  • Issue created by @thecooltechguy
  • Status changed to Postponed: needs info over 1 year ago
  • I am pasting my comment from SE:

    Are there errors in browser console? What, technically, does "works" and "does not work" in this question? Can you be more precise about that aspect please?

  • There are no errors in the browser console.

    By "works" and "does not work", I meant that the conditional behavior of the elements isn't working. For example, say that I make an element that is conditionally disabled when a checkbox is checked. When I view the webform in the View or Test pages, it works as expected -- when I check the checkbox, the element is disabled, and vice versa. However, when I embed the webform as described in my issue, this no longer works: if I check the checkbox, the element doesn't become disabled, etc.

    This is an example of conditional logic that doesn't work for webform elements when rendered/embedded via Drupal.

    Thanks for your help, and lmk if I can further clarify anything!

  • Does the form have to be multi-page to exhibit the bug?

  • When embedding single-page forms using the render array approach explained above:

    1. If I do #lazy => FALSE, conditional logic doesn't work
    2. If I do #lazy => TRUE, conditional logic works

    When embedding multi-page forms using the render array approach explained above:

    1. If I do #lazy => FALSE, conditional logic doesn't work on any page
    2. If I do #lazy => TRUE, conditional logic works ONLY on the first page. It does not work on any subsequent page.

    Side note:
    I also noticed that there is no page refresh when going between pages in a webform embedded with a render array, unlike what happens in a block-embedded webform, where the page is refreshed when going between pages -- and conditional logic works in blocks.

    So I think that whether or not the page refreshes has something to do with whether or not the conditional logic works, etc. I'm not 100% sure on this, just a suspicion.

  • Status changed to Active over 1 year ago
  • Hi! Just wanted to see if there are any solutions/possible workarounds for this issue, that I could try? thanks!

  • πŸ‡¨πŸ‡΄Colombia Ozle BogotΓ‘

    Hi @thecooltechguy,

    I was able to replicate this problem and found that if you enable Ajax on your web form it works correctly:

    without Ajax:

    Using Ajax:

  • πŸ‡ΊπŸ‡ΈUnited States alphex Atlanta, GA USA

    I can verify that this problem exists ALSO on webforms that are loaded by 'webform reference' fields.

    On a number of my sites, I have content management activities where admins will create a page, and place a paragraph type that allows them to select from a series of webforms, that get displayed ont he page ...

    When built on a page that way, the conditional logic does not work.

    It does work when we got to the "webform" "view" directly.

  • Status changed to Closed: won't fix 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    Your code is nesting a webform inside a form, and it won't work as expected.

    If you place the webform via a controller, it will work as expected.

Production build 0.71.5 2024