Skip to content

Add pleaseChoose option to SelectElement#200

Open
jrauh01 wants to merge 1 commit into
mainfrom
feature/default-please-choose-option
Open

Add pleaseChoose option to SelectElement#200
jrauh01 wants to merge 1 commit into
mainfrom
feature/default-please-choose-option

Conversation

@jrauh01
Copy link
Copy Markdown
Contributor

@jrauh01 jrauh01 commented May 27, 2026

Add a pleaseChoose option to SelectElement that prepends the placeholder automatically:

(new SelectElement('type'))
    ->setOptions($types)
    ->setPleaseChoose(true);

Or declaratively:

[
    'type'         => 'select',
    'options'      => $types,
    'pleaseChoose' => true,
]

When enabled and no '' option does exist, the element prepends ['' => sprintf(' - %s - ', t('Please choose'))] to the rendered options and marks it as disabled, so callers do not need to handle either step.

resolve #199

When set, a disabled " - Please choose - " option with an empty value is
prepended automatically, so callers no longer need to merge it into the
options array and separately add '' to `disabledOptions`.

The option is skipped when the options array already contains a '' key,
so existing callers that manage their own placeholder are unaffected.
@jrauh01 jrauh01 self-assigned this May 27, 2026
@cla-bot cla-bot Bot added the cla/signed label May 27, 2026
@jrauh01 jrauh01 requested a review from Copilot May 27, 2026 08:53
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a pleaseChoose option to SelectElement that lazily prepends a disabled '' placeholder option (" - Please choose - ") at assembly time, both via a fluent setter and a declarative attribute. The change centralizes a repeated pattern previously duplicated across forms (issue #199).

Changes:

  • New pleaseChoose property with setPleaseChoose() setter.
  • assemble() prepends a disabled SelectOption with empty value when pleaseChoose is enabled and no top-level '' option already exists; selection callback reuses isSelectedOption('').
  • Registers a pleaseChoose declarative attribute setter callback (matching the first-class-callable style already used elsewhere) and adds four tests covering rendering, value-selection, non-override, and lazy fluent API behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/FormElement/SelectElement.php Adds $pleaseChoose flag, setter, assembly-time placeholder injection, and declarative attribute callback.
tests/FormElement/SelectElementTest.php Adds four tests covering prepend rendering, deselection when a value is set, no-override of existing empty option, and lazy fluent-API behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jrauh01 jrauh01 requested a review from lippserd May 29, 2026 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let SelectElement support a built-in default "Please choose" option

2 participants