Skip to content

Fix: accept a single value in multiple selects and checkboxes - #123

Closed
Contributolo wants to merge 4 commits into
inline0:mainfrom
Contributolo:fix/multiple-select-single-value
Closed

Contributolo wants to merge 4 commits into
inline0:mainfrom
Contributolo:fix/multiple-select-single-value

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Problem

A select with multiple: true and a checkbox field store a list of {value, label} options. Two other stored shapes occur in real content:

  1. A single {value, label} object, saved while the field was still a single select, before multiple was enabled.
  2. A plain string or number written outside the editor by scripts, the REST API or migrations, for example "language":"de".

Symptoms:

  • Editor: a plain value crashes the field ((value || []).filter is not a function in the multiple select, v.map is not a function in the checkbox).
  • Editor: with a single object, the checkbox shows no option as checked, and choosing a second option in either field replaces the stored option instead of adding to it.
  • Server: a plain value raises foreach() argument must be of type array|object and renders as false. A single object is iterated as two selections (its value and its label), which duplicates it when both are equal, for example with plain string options.

Fix

One helper per side reads the stored value as a list:

  • normalizeOptionValues() in src/blocks/utils/option-values.ts, used by the multiple select, the checkbox and the add/remove logic in getValue().
  • Block::multiple_option_values(), used by transform_option_attribute(), including multiple selects with populate.type: "fetch".

A list is returned unchanged, a single option object becomes a one-item list, a plain string or number becomes one option (labelled from the field options in the editor, falling back to the value), and empty values become an empty list. Lists behave exactly as before. Stored content is not rewritten on load; the field saves a list once the user changes it.

Tests

  • tests/unit/block/BlockTest.php: data-provider test for transform_attributes() covering string, number, numeric string, object, list and empty values for multiple selects, checkboxes and fetch selects. 10 of the 19 cases fail on main and all pass with this change.
  • tests/client/option-values.test.ts: unit tests for the helper.

Checked locally with the new tests outside wp-env, tsc --noEmit, eslint, prettier, PHPCS and PHPStan level 3. The editor itself was checked by rendering the field components server side (multiple select and checkbox with a string, a number and a single object), not in a browser.

A select with `multiple: true` and a checkbox field store a list of
`{value, label}` options. Two other shapes occur in real content:

- a single `{value, label}` object, saved while the field was still a
  single select, before `multiple` was enabled
- a plain string or number, written outside the editor by scripts, the
  REST API or content tooling, for example `"language":"de"`

In the editor a plain value crashed the field: the multiple select
called `filter` and the checkbox called `map` on it ("is not a
function"). With a single object the checkbox showed no option as
checked, and choosing a second option in either field replaced the
stored option instead of adding to it.

On the server a plain value raised a `foreach()` warning and rendered
as `false`. A single object was iterated as two selections, its value
and its label, which duplicated it when both are equal.

Both sides now read the stored value through one helper each,
`normalizeOptionValues()` and `Block::multiple_option_values()`: a list
is kept as it is, a single option object becomes a one item list, a
plain value becomes one option (labelled from the field options in the
editor) and empty values become an empty list. Lists behave exactly as
before.

@dnnsjsk dnnsjsk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The normalization addresses the reported crashes, but I reproduced a remaining data-loss path in the numeric scalar case through the actual getValue() implementation: start with scalar 0 and numeric options 0/1, select 1, then uncheck 0. Selecting 1 correctly produces [0,1] with this PR, but unchecking 0 returns [] instead of preserving 1. Checkbox passes itemInner.value directly to change(), so the numeric 0 reaches the value ? toggleOption() : [] guard in Fields and is treated as a clear-all command.

Please treat zero as a valid option value in the edit path and add a regression covering that sequence. The pure normalization tests do not cover add/remove behavior in Fields. Browser regression coverage is also needed for scalar and single-object values in multiple selects/checkboxes: show the existing selection, add/remove without losing it or sibling values, and preserve stored shapes on an untouched save/reload. The current component SSR checks cannot verify those editor interactions. Leaving this open for the corrections rather than merging it into the tested 7.7.2 cut.

The checkbox passes the clicked option value straight to change(), and
Fields only toggled it when the value was truthy (`value ? toggleOption()
: []`). An option value of 0 was therefore read as "clear everything":
starting from a stored 0 with the options 0 and 1, selecting 1 gave
[0, 1], and unchecking 0 gave [] instead of [1].

The add and remove logic is now toggleOptionValue() in
src/blocks/utils/option-values.ts, and getValue() calls it directly, so
the unit tests run the same code as the editor. Every option value
toggles, including 0, "0", "" and false. Clearing all options keeps
working as before: the toggle-all checkbox and removing the last card
send a direct change, which never reaches getValue(), and undefined or
null still clear.

Option objects with a falsy value broke one step earlier. optionSetter()
and the option lookup in Fields read options with `e?.value || e`, so
`{ value: 0 }` turned into the whole object: the checkbox never showed it
as checked and toggling it stored a nested object. The multiple select
filtered its cards with `e?.value`, so a selected 0 was not shown and
removing a sibling card wrote the list without it. Option values and
labels are now read with getOptionValue() and getOptionLabel(), and
selections are compared by their string form, as the server does.

Labels reach the controls as text. An option 0 had no visible checkbox
label, and plain numeric options crashed the multiple select because
ComboboxControl calls string methods on labels.

A stored list of plain values, as scripts write it, is read as options
too, so the editor shows it and toggles it like any other list.
…kboxes

transform_option_attribute() kept only truthy resolved values, so a
selected option 0 or "0" disappeared from the template data, and
transform_attributes() skipped a stored integer 0 as empty before it
could be read as a selection. Only unresolved values (false and null)
are dropped now, and an integer 0 counts as a stored value for
checkboxes and multiple selects. Single selects and other fields keep
their current handling of 0.
…n the editor

Adds the test block blockstudio/type-option-values with checkboxes and
multiple selects for numeric and string options, declared as objects and
as plain values, each starting with 0 or "0" stored.

For a stored scalar and for a stored single option object, the browser
test checks that the editor shows the stored selection, that an
untouched save and reload keeps the stored attributes exactly (compared
with the saved post content), that adding a second option keeps the
stored one, and that removing the stored option keeps the added one,
including after save and reload. The rendered values are checked at
every step.
@Contributolo

Copy link
Copy Markdown
Collaborator Author

Thanks for the thorough review, you were right. Changes are in three new commits (9fbe9d8, 8da67c4, 95f497a):

Zero in the edit path. The checkbox passes itemInner.value to change(), and value ? toggleOption() : [] read 0 as "clear all". The add/remove logic is now toggleOptionValue() in src/blocks/utils/option-values.ts, called directly by getValue(), so every option value toggles (0, "0", "", false). The real clear-all path is unchanged: toggle-all sends change([], true) and removing the last card sends change(false, true), both direct changes that never reach getValue(). undefined/null still clear.

Testing the other falsy values turned up four more spots on the same path, all fixed:

  • optionSetter()/finder() used e?.value || e, so { value: 0 } became the whole object. It never showed as checked and toggling stored a nested object.
  • The multiple select filtered its cards with e?.value, hiding a selected 0 and dropping it when a sibling card was removed.
  • Labels now reach the controls as text. A 0 option had no checkbox label, and plain numeric options crashed the multiple select (ComboboxControl calls string methods on labels; this happens on main too).
  • Server side, if ( $val ) dropped resolved 0/"0", and an integer 0 was skipped as empty before being read as a selection (only for checkboxes and multiple selects; single selects are unchanged).

Regression for your sequence. tests/client/option-values.test.ts runs the function Fields runs, with the value the checkbox passes: scalar 0, options 0/1, select 1 → [0, 1], uncheck 0 → [1]. It also covers a single { value: 0 } object, string options "0"/"1", "", false, "NaN", sibling removal and the null clear. Against edf52df's getValue() the same sequence gives [] (numbers) and a nested object (option objects). BlockTest gains 18 zero cases.

Browser coverage. tests/e2e/types/option-values.ts with a new test block blockstudio/type-option-values: checkboxes and multiple selects with numeric and string options, as objects and as plain values, all starting at 0/"0". For a stored scalar and a stored single option object it checks that the editor shows the stored selection, that an untouched save and reload keeps the stored attributes exactly (compared with the parsed post content), that adding a second option keeps the stored one, and that removing the stored option keeps the added one, also after save and reload. Rendered values are asserted at each step. On main and on edf52df the block crashes; restoring only the old guard fails exactly at "uncheck 0".

Verified locally in WordPress Playground (no Docker here): new E2E 6/6, related E2E suites unchanged (select-fetch needs its hardcoded :8888), BlockTest 61/61, npm run check, composer cs and PHPStan clean. The committed editor bundle is not rebuilt in this branch, so the browser tests need npm run build first.

Not changed here (separate from this PR, happy to follow up): 0 in single/stylised selects (value?.value || value), radio deselect, allowReset with a default of 0, and the fetch-select filters.

@dnnsjsk

dnnsjsk commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Included in Blockstudio 7.7.4.

Support for imported scalar and single-option values in multiple selects and checkboxes has shipped, including numeric-zero and repeater regressions.

Your changes were incorporated into the consolidated release branch with integration tests and release adjustments, so I am closing this PR as shipped rather than merging the original branch again. Full CI (14/14) and Distribution (5/5) passed on release commit e1d38df. Thank you for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants