Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x]: Dropdown field appears blank even with a default set #13034

Closed
mjniland1 opened this issue Apr 3, 2023 · 7 comments
Closed

[4.x]: Dropdown field appears blank even with a default set #13034

mjniland1 opened this issue Apr 3, 2023 · 7 comments

Comments

@mjniland1
Copy link

What happened?

Description

The default dropdown property is ignored when adding a new dropdown field to an entry. This isn't a huge issue for one-off dropdowns but if you have a matrix/neo field layout, you have to then set all of the defaults manually on the entry.

Steps to reproduce

  1. Create a new dropdown field
  2. Add a few sample dropdown options to this field
  3. Set one of the options as the default
  4. Add the dropdown field to an entry

Expected behavior

The new dropdown field on the entry should have the default applied therefore not requiring a user to make a selection.

Actual behavior

The new dropdown field appears blank and throws a "Field is Invalid" error when a user attempts to save.

Craft CMS version

Craft Pro 4.4.5

PHP version

8.0.8

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Batch Actions 1.3.0
Contact Form 3.0.1
Contact Form Extensions 4.2.2
Control Panel CSS 2.6.0
CP Field Inspect 1.4.4
Embedded Assets 3.1.3
Empty Coalesce 4.0.0
Neo 3.7.5
Quick Field 2.0.3
Redactor 3.0.4
Table Maker 4.0.4

@brandonkelly
Copy link
Member

This is working as expected. The default value will only come into play for new entries, where we definitely know that the existing null value was not intentional.

You can bulk-set a specific value on your existing entries using the resave/entries command. See this knowledge base article for full details on how to use it.

@linusschwab
Copy link

@brandonkelly In my opinion, it makes sense to add an empty option to new dropdown fields with no default value on existing entries to "force" the user to make a decision and select a value. But for new fields with a default value set, this is really annoying.

Are you sure that this is intended behaviour? If I understand correctly, the idea in #12235 was to only add this behaviour for new fields on existing entries with no default value set.

Of course I could use the resave command or write a simple content migration every time a new dropdown field with a default value is added. But this is just unnecessary work in my opinion and was working as expected before the 4.4 update. I don't mind that existing entries have a null value before they are resaved again - just that it throws an error when not explicitly choosing a value even though there is a default value set.

@brandonkelly
Copy link
Member

@linusschwab The default option has never come into play for existing elements; only brand new ones.

@linusschwab
Copy link

@brandonkelly Oh, good to know, thanks for the info. Then this just worked in the past because the first option was likely often set as the default value in our case...

Then I will either just set the default option as null or still write a simple migration (or use the CLI) to set the value for existing entries.

But wouldn't a behaviour like that make sense? Basically to pre-select the default option (if one is set) in the UI for existing entries instead of throwing an error on save. Otherwise there is also no indication to the user which of the options is the default one.

@brandonkelly
Copy link
Member

@linusschwab The tricky thing is that it’s not extremely clear whether a null value was intentional, or just because the field didn’t exist the last time the entry was edited. It’s ambiguous. (Even if there’s no valid null option, it could be the case that the field used to have an empty option, when the entry was last edited.)

So that’s why we just don’t try to be smart, and only actually use the default value for completely new entries. And give you the resave/entries command with --set and --to options, if you want to apply the default value to existing elements.

@linusschwab
Copy link

@brandonkelly Thanks a lot for the detailed answer. I can see your point, especially if the smart behaviour does not work as expected.

Personally I still think that this would be less of a problem, at least in our case it happens much more often that a new dropdown field is added than an existing (null) option is removed. In that case it would also be possible to execute the console command or write a migration.

It's great to have the console commands, but for more complex scenarios (matrix or super table fields) and to make sure that it gets executed automatically during the deployment, I would still use migrations over them personally.

@brandonkelly
Copy link
Member

Craft 4.5.0 is out with an improvement to Dropdown field behavior: now if a Dropdown field’s value is invalid, and the field contains an explicit Default option, the default option will be auto-selected, and the field will be marked as changed. (#13540)

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

No branches or pull requests

3 participants