Hidden steps values are not removed

Created on 22 February 2023, over 1 year ago
Updated 17 October 2023, 9 months ago

Problem/Motivation

When a multistep Webform has skip-step conditions, if we go back, update a value and go forward again, these values are not deleted.

Steps to reproduce

Create a 3-step webform with one survey question each.

Step 1 a numerical value from 0 to 10.
Step 2 a radio that ask whether we should skip step 3 or not.
Step 3 a simple textfield.

In step 2 we put a condition of visible if the value of step 1 is less than or equal to 2.
In step 3 we put a condition of invisible if step 2 has a value of "yes".

--

We access to the form, to question 1 we give a value of 2, when submit we can see step 2, select "Yes" and go back to step 1 and give a value of 5 and submit. Step 2 will not be shown because it has a value greater than 2 and will go directly to the thank you page without going through step 3.

I'm attaching this an example to reproduce this bug.

πŸ› Bug report
Status

Active

Version

6.2

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain facine

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

Comments & Activities

  • Issue created by @facine
  • πŸ‡ͺπŸ‡ΈSpain facine

    For now my workaround was:

    /**
     * Implements hook_webform_submission_form_alter().
     */
    function mymodule_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
      if (\Drupal::service('router.admin_context')->isAdminRoute()) {
        return FALSE;
      }
    
      if (!empty($form['actions']['wizard_prev']['#submit'])) {
        array_unshift(
          $form['actions']['wizard_prev']['#submit'],
          'mymodule_webform_submission_form_alter_wizard_prev_submit'
        );
      }
    }
    
    /**
     * Submit callback for the previous page submit.
     */
    function mymodule_webform_submission_form_alter_wizard_prev_submit($form, FormStateInterface $form_state) {
      $current_page = $form_state->get('current_page');
    
      /** @var \Drupal\webform\WebformSubmissionForm $form_object */
      $form_object = $form_state->getFormObject();
      /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
      $webform_submission = $form_object->getEntity();
      $webform = $webform_submission->getWebform();
      foreach ($webform->getElementsInitializedAndFlattened() as $key => $element) {
        if (!empty($element['#webform_parents']) && in_array($current_page, $element['#webform_parents'])) {
          // Clear value for elements of the current page.
          $form_state->setValue($key, '');
        }
      }
    }
    
Production build 0.69.0 2024