Using crm_optgroup for headers in select lists causes the first option to be selected by default when the list is single select
Overview
I noticed this issue yesterday and tested it on https://demo.circle-interactive.co.uk/ to make sure it wasn't just my installation. If I use crm_optgroup to define headers in a select list of options in a custom field and that list is single select it doesn't work as expected. Rather than showing the "- Select [field label] -" it defaults to the first item under the crm_optgroup. This results in users accidentally submitting that if they are entering data in other fields.
Reproduction steps
- Create a custom select field that will be single select
- Add a crm_optgroup to the select options with some options below the optgroup
Current behaviour
The first option in the topmost crm_optgroup is selected as the default
Expected behaviour
It should show "- select [field label] -" instead in the field so that users can leave it blank when needed.
Environment information
Tested this in a couple of CiviCRM environments. Specific versions tested were 5.66.2 and 5.69.2 Tested in Firefox and Safari
Comments
Removing the optgroups (or making the list multiselect) corrects the problem.