As request by a client of mine, some unchecked checkboxes should not show up in the submission values.

I've added an option in the Checkbox webform element: "Treat unchecked as empty" and overridden 'hasValue' to respect this option.

A patch will be added in the first comment.

Comments

SpadXIII created an issue. See original summary.

spadxiii’s picture

Status: Active » Needs review
StatusFileSize
new1.5 KB
spadxiii’s picture

StatusFileSize
new1.51 KB

Forgot to make the field title translatable... so new patch!

jrockowitz’s picture

We already have an 'Exclude empty elements' setting for
the Preview page (/admin/structure/webform/manage/contact/settins/form) and Email handlers (/admin/structure/webform/manage/contact/handlers)

The exclude empty behavior only excludes empty strings and does not exclude a checkbox set to FALSE or number input set to '0'.
@see \Drupal\webform\Plugin\WebformElementBase::build

I think we could enhance/piggy-back on this existing feature. Maybe we should added an '#exclude_empty' property to all elements that is stricter and specific to the element's data type. For example, a checkbox set to FALSE would be considered empty, and a number input set to '0' would be considered empty.

Some notes/thoughts...

  • Preview and Email excluded empty description should note that per element exclusion is available.
  • Must create a test form that checks #exclude_empty support for text inputs, checkbox, and number inputs
  • Might need to add protected WebformElementBase::isExcluded method to better manage this feature.
  • The 'Exclude empty element' setting should be 'Advanced' element tag under 'Submission Display'
spadxiii’s picture

The 'Exclude empty elements' was not sufficient for unchecked checkboxes, because they have 2 values: 1 and 0 (or Yes and No) from the boolean element. It's not possible for the current checkbox to get the value of an empty string.

I think it's a good idea to have some configuration option per element to control what is empty.

About your last note/thought: I'm sure there are use-cases to be found where you would exclude an unchecked checkbox in one email, but not another. If you move the exclude-setting to the element, it would be excluded in all handlers. The way it currently works is fine, except that some elements need more control over what is considered "empty".

jrockowitz’s picture

The most immediate work-around is to use 'Checkboxes' with only a single option.

checkboxes:
  '#type': checkboxes
  '#title': checkboxes
  '#options':
    'yes': 'Check this box if you say ''Yes'''
jrockowitz’s picture

Status: Needs review » Closed (won't fix)

The workaround in #6 is a good immediate solution otherwise I think this feature should be handled via custom code.

jrockowitz’s picture

@SpadXIII I did wind up implementing this feature and using your patch as the starting point.

@see #2977378: Add 'exclude unselected checkboxes' from email notification and preview.

electrokate’s picture

@jrockowitz, for the life of me I cannot find where to edit this setting. I see that you added the patch in #3, can you tell me how to exclude empty checkboxes? I have looked everywhere and it is not visible to me.

jrockowitz’s picture

StatusFileSize
new212.15 KB