Problem/Motivation
I need to make the "- Select -" option available by default for non-required fields in a Drupal Form. But can't do this for some reason, seems this is a bug in the Drupal Form API.
Steps to reproduce
1. Create a Drupal form like this:
public function buildForm(array $form, FormStateInterface $form_state) {
$form['val1'] = [
'#title' => 'Val1',
'#type' => 'select',
'#options' => ['one', 'two', 'three'],
'#required' => TRUE,
];
$form['val2'] = [
'#title' => 'Val2',
'#type' => 'select',
'#options' => ['one', 'two', 'three'],
'#required' => FALSE,
];
return $form;
2. See that the "- Select -" option is available for the required field "Val1"
But not available for the non-required field "Val2":


Proposed resolution
I see no real reasons for this strange behavior, even the opposite - for the non-required fields we should provide the ability to select "nothing".
So, looks like we should find the source of this strange behavior, and provide a fix for it.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| ksnip_20250508-151659.png | 9.15 KB | murz | |
| ksnip_20250508-151657.png | 8.41 KB | murz |
Comments
Comment #2
cilefen commentedI think the setting you are looking for is
#empty_value.Its documentation reads:
Comment #3
cilefen commentedThere is a @todo below that documentation saying "address the above issue in Drupal 8", so I guess that hasn't yet happened. I hope there is an issue open somewhere to make this "just work" by default.
Comment #4
cilefen commentedI am relating a few issues.
Comment #6
quietone commented@cilefen, has provided a solution here and indicated the existing issues to fix this behavior. So, I think this cab be closed in favor of those issues. Any further discussion should happen in one of those.
I am closing this per the guidance in Handle or refer a support request in an issue.