Skip to content

[6.x] Keep blank Dropdown option values - #20140

Merged
riasvdv merged 2 commits into
6.xfrom
bugfix/cms-2441-dropdown-fields-empty-values
Oct 29, 2026
Merged

riasvdv merged 2 commits into
6.xfrom
bugfix/cms-2441-dropdown-fields-empty-values

Conversation

@i-just

@i-just i-just commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Initial issue: if you created a dropdown field with an empty value (e.g., Please select), added that field to an entry type, and tried to create an entry of that type, an exception was thrown:

ErrorException
packages/cms/src/Field/Dropdown.php:138
Undefined array key "value"

Claude's explanation:

A Dropdown option with a blank value lost its value key on save, so creating an entry failed with Undefined array key "value". Once that was fixed, the entry form showed a second, unlabeled blank option above the field's own blank option.

save field settings
  ConvertEmptyStringsToNull: value "" → null
  null key dropped on save            → {"label":"Please select"}   ✗ crash on entry create
+ BaseOptionsField::__construct: label/value ?? ''  → {"label":"Please select","value":""}

render entry form
  Dropdown::encodeValue: "" → "__blank__"
  Choice: no option with value "" → prepend {label:"", value:""}   ✗ duplicate blank
+ Dropdown::uiControl: field has its own blank option → withoutPlaceholder()

Normalizing in the constructor also repairs fields that were already saved without a value key when they're loaded, so no migration is needed.

Evidence

tests/Feature/Field/OptionsFieldEmptyValueTest.php

  • Before:
    ⨯ an option saved without a value is treated as a blank option     Undefined array key "value"
    ⨯ a blank option value posted as null is saved as an empty string  Undefined array key "value"
    ⨯ a dropdown with its own blank option does not get another one    Undefined array key "placeholder"
    
    After:
    ✓ an option saved without a value is treated as a blank option
    ✓ a blank option value posted as null is saved as an empty string
    ✓ a dropdown with its own blank option does not get another one
    ✓ a dropdown without a blank option keeps the leading blank
    

🤖 Generated with Claude Code

A blank option value is posted as null (ConvertEmptyStringsToNull) and
dropped on save, so the option lost its "value" key and creating an entry
failed with "Undefined array key". Options now default label and value to
an empty string.

Dropdown also encodes a blank value as __blank__, so the Choice control
didn't see the field's own blank option and prepended another one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@linear-code

linear-code Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

CMS-2441

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

📚 Storybook previews

@craftcms/ui — open Storybook

No changed components detected in this Storybook.

resources/js — open Storybook

No changed components detected in this Storybook.

@i-just
i-just requested a review from riasvdv October 29, 2026 07:22
@riasvdv
riasvdv merged commit ff565ed into 6.x Oct 29, 2026
9 checks passed
@riasvdv
riasvdv deleted the bugfix/cms-2441-dropdown-fields-empty-values branch October 29, 2026 07:42
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