Elements that are hidden via states should not be visible on the preview page. It's confusing to the user to see fields there that he hasn't seen or filled out in the form.

Example form:

trigger:
  '#type': checkbox
  '#title': Trigger
hidden:
  '#type': checkbox
  '#title': Hidden
  '#states':
    visible:
      ':input[name="trigger"]':
        checked: true

Preview page will show: "Hidden: No"

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Ice-D created an issue. See original summary.

jrockowitz’s picture

I don't think hidden elements should support #states.

Hidden elements can currently be excluded from the preview page and email.

jrockowitz’s picture

I miss read the issue. I don't see any easy solution.

Ice-D’s picture

Sorry, by "hidden" I meant "invisible". Will change the title. Do you think it is an issue worth tackling?

Ice-D’s picture

Title: Hidden elements should be hidden on preview page » Invisible elements should be hidden on preview page
Ice-D’s picture

The problem mainly concerns checkboxes and radios. Other elements can be hidden on the preview page if empty. (Invisble elements are "empty" to webform)

Can we have an option to hide checkbox/radio elements on the preview if they are not checked?

jrockowitz’s picture

There is definitely an issue here because a hidden checkbox should be stored as a NULL value and a visible unchecked checkbox should be stored as FALSE.

Ice-D’s picture

There is definitely an issue here because a hidden checkbox should be stored as a NULL value and a visible unchecked checkbox should be stored as FALSE.

Any new insights on this?

jrockowitz’s picture

Nullifying conditionally hidden elements is going to require a bit of work and a lot of testing. If we are not careful we could cause people to lose data.

nodecode’s picture

My forms have quite a few conditionally-hidden elements and the Preview/Submissions are fairly indecipherable both for the person filling out the form and the person receiving the submission. I'm not the best at debugging code but I do understand it and I'm willing to look into it if you need a hand. Otherwise I'm watching this issue closely and here to test thoroughly.

Ice-D’s picture

My temporary solution is to hide all the checkboxes that aren't checked. Just put this in a custom module.

function MY_MODLE_preprocess_webform_element_base_html(&$variables) {
  if(isset($variables['item'])) {
    $element = $variables['element'];

    if($element['#type'] == 'checkbox') {
      $submission_data = $variables['webform_submission']->getData();
      $name = $variables['item']['#name'];
      if(empty($submission_data[$name])) {
        unset($variables['item']);
      }
    }
  }
}
nodecode’s picture

@Ice-D, for me it's also affecting Text fields which contain default values but are hidden so they shouldn't be included in the Preview/Submission.

@jrockowitz, If you're wary about this being fixed and people losing data, perhaps there could be a checkbox in the settings to "Exclude hidden elements" from the Preview they same way there exists "Exclude empty elements". Just a thought.

jrockowitz’s picture

Status: Active » Needs review
FileSize
4.64 KB
3.68 KB

Attached is a test webform and a patch that checks if an element is conditionally hidden when it is being viewed.

It is possible that this pretty basic patch could solve a lot of problems. Once all the existing tests pass I will add some additional test coverage.

  • jrockowitz committed af4d2c2 on 2939414-invisible-elements-preview
    Issue #2939414 by jrockowitz: Invisible elements should be hidden on...

  • jrockowitz committed 304d526 on 2939414-invisible-elements-preview
    Issue #2939414 by jrockowitz: Invisible elements should be hidden on...
jrockowitz’s picture

  • jrockowitz committed 64da708 on 2939414-invisible-elements-preview
    Issue #2939414 by jrockowitz: Invisible elements should be hidden on...
jrockowitz’s picture

  • jrockowitz committed bbc53f1 on 8.x-5.x
    Issue #2939414 by jrockowitz: Invisible elements should be hidden on...
jrockowitz’s picture

Status: Needs review » Fixed
jrockowitz’s picture

I committed the patch. Please download the latest dev release to review.

nodecode’s picture

It seems to be working well in my tests of more complex forms.

jrockowitz’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.