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]: Can't omit value for optional Dropdown field in 4.9.0 #14912

Closed
boboldehampsink opened this issue May 1, 2024 · 7 comments
Closed

[4.x]: Can't omit value for optional Dropdown field in 4.9.0 #14912

boboldehampsink opened this issue May 1, 2024 · 7 comments
Assignees

Comments

@boboldehampsink
Copy link
Contributor

boboldehampsink commented May 1, 2024

What happened?

Description

In 4.9.0 I get the following error:

web-1  | 2024-05-01 08:13:58 [console.INFO] [craft\services\Elements::_saveElementInternal] Element not saved due to validation error: Array
web-1  | (
web-1  |     [toolType] => Array
web-1  |         (
web-1  |             [0] => Type is invalid.
web-1  |         )
web-1  | 
web-1  | )

I'm not setting any value to this field and it is not required. It doesn't have a default either. This worked in 4.5.x

Steps to reproduce

  1. Add a dropdown field without default value
  2. Add it to a fieldlayout with it being optional
  3. Programmatically save an element without dropdown value

Expected behavior

No validation whatsoever

Actual behavior

Validation: "xxx is invalid"

Craft CMS version

4.9.0

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@i-just
Copy link
Contributor

i-just commented May 1, 2024

Hi, thanks for reaching out!
I’m having trouble replicating this behaviour. Could you please share the list of installed plugins and their versions and also explain how you’re programmatically saving the element?

@boboldehampsink
Copy link
Contributor Author

We're saving the element via fixtures. I have seen this error in multiple, independent setups of ours that use a dropdown filled via fixtures.

@brandonkelly
Copy link
Member

@boboldehampsink Can you update to the latest 4.x release and see if that fixes it?

@boboldehampsink
Copy link
Contributor Author

@brandonkelly oops I wrote 4.6.0 but I meant 4.9.0 - this IS on the latest version

@boboldehampsink boboldehampsink changed the title [4.x]: Can't omit value for optional Dropdown field in 4.6.0 [4.x]: Can't omit value for optional Dropdown field in 4.9.0 May 2, 2024
@brandonkelly
Copy link
Member

Can you send us a project where this is happening, so we can reproduce? [email protected]

@studio-stomp
Copy link

studio-stomp commented May 9, 2024

I'm running into this issue too. As it happens I was working on a CLI-based sync command, that also does a call to \Craft::$app->getElements()->saveElement($element);. And I decided to update from 4.8.9 to 4.9.2 after working on this command and testing it locally, and before deploying to our test server. And then there it failed, so it threw me off initially as a difference between configs. But I can reproduce locally.

So very narrowly, the reason why it did work for me on <4.9.0 and not in >=4.9.0 is this change: 4.8.11...4.9.0#diff-9490c3633b0153e774e90992895f3d5ae500ce1f657afd0da3eb2950400b4498. And since fixtures are usually run from a CLI-context as well, I suspect this is also why @boboldehampsink ran into this issue and reported it.

Side-note: there is a bit more to it for my project I suspect, since I get this validation message also on existing entries when saving them through the CMS. They don't get touched often, and when they do the person managing the content probably fixes the validation message by selecting a value and never reported it to us.

Edit: I "fixed" it by added an empty value as the default option to the field. That works fine enough for our project.

@angrybrad angrybrad self-assigned this May 13, 2024
@angrybrad
Copy link
Member

The behavior has changed a bit over time (especially in 4.5.x), but you cannot save a dropdown field without one of its options being selected as a value while there is no default value set.

Context here with lots of linked related issues: #13540

If you want to save an entry without setting an option, add an empty value as the default option to the field, and that will be used as the default when there is none set.

This is reproducible (and expected behavior) from the control panel as well.

CleanShot 2024-05-13 at 16 46 46

To me, the odd part is that you mentioned it was working fine in 4.8.9 @studio-stomp, which I would assume had the same behavior.

I am going to go ahead and close this as intended behavior, but if there are additional details, follow up here again, and we can re-open if necessary.

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

5 participants