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

[5.x]: Getting value invalid on save for existing entries after adding new dropdown field #14731

Closed
mortenta opened this issue Mar 30, 2024 · 18 comments
Labels

Comments

@mortenta
Copy link

What happened?

Description

After adding a new dropdown field to an entry type that is already in use by entries, I get an error on save saying that the field is invalid, although the default value is a seemingly selected. The only way to get past the error is to select another value, then go back and select the default value.
However, if used in a matrix, and the sub elements are inline, it seems you only have to re-select one place in order to be able to save. However, if not inline, you will have to change each element.

Steps to reproduce

  1. Have a section, an entry type with fields, and an entry
  2. Add a new field with field type dropdown to the entry type, with more than one option, and one of the options set as default.
  3. Go to the entry, and make a change to one of the other fields, and try to save
  4. The invalid error should appear. Try to change the value of the new dropdown, and save.

Expected behavior

The default value of the new dropdown(s), as shown in the UI, should be saved.

Actual behavior

An error is thrown, saying that the new dropdown field(s) are invalid.

Craft CMS version

5.0.0

PHP version

8.2.16

Operating system and version

MacOS 14

Database type and version

MySQL 8.2.0

Image driver and version

GD 8.2.16

Installed plugins and versions

Blitz 5.0.0-beta.4
CKEditor 4.0.3
Feed Me 6.x-dev
Pexels 1.0.1
SEOmatic 5.0.0-beta.8

@mortenta mortenta added the bug label Mar 30, 2024
@brandonkelly
Copy link
Member

I’m not able to reproduce this. For me it’s working exactly as described in #13540, where the field is initially marked as changed when loading the entry, with the default value preselected, and the field is saved with that value once I save the entry, regardless of whether I make changes to other fields as well.

Are you able to reproduce this consistently? Can you think of anything else that should be added to the steps to reproduce?

@i-just
Copy link
Contributor

i-just commented Apr 2, 2024

I was able to replicate this, but only if I added a dropdown field (with a default value selected) to an entry that uses a matrix field with inline-editable blocks. I raised a PR for this.

Adding a dropdown field to an entry outside of a matrix field or to a matrix field that uses another view mode (e.g. cards) works as expected for me. If you’re experiencing the issue without the matrix field or in a matrix field that uses a view mode other than inline-editable blocks, please provide some additional info on how to replicate it, and I’ll look into that as well.

@mortenta
Copy link
Author

mortenta commented Apr 2, 2024

I have reproduced it with my setup in matrix fields, however, it's in two matrix levels:

  1. Field : "Block Builder" (matrix) with entry types: "Block : Cards Container" inline
  2. Entry type "Block : Cards Container" has field: "Block : Cards Container : Card" (matrix) with entry types: "Block : Cards Container : Card" as card

I have reproduced the error as follows:

  1. Got to entry type "Block : Cards Container : Card"

  2. Create a new dropdown field (I called it RandomDropDown) with the options a, b and c. Option b is the default (in case the error is connected to not having the first option as the default value)

  3. Save field, add to entry type and save

  4. Go to an entry that is already using these matrix fields with these entry types.

  5. Try to save without doing any changes, and receive the error:
    Found 4 errors [RandomDropDown is invalid.](http://public2.dev.itefy.net/admin#) [RandomDropDown is invalid.](http://public2.dev.itefy.net/admin#) [RandomDropDown is invalid.](http://public2.dev.itefy.net/admin#) [RandomDropDown is invalid.](http://public2.dev.itefy.net/admin#)

  6. If opening one entry card containing RandomDropDown and saving it (without any changes), then on main entry save, the error for this sub entry disappears.

  7. If opening another entry card containing RandomDropDown, but not saving it, just closing it, the save the main entry, the error remains.

@brandonkelly
Copy link
Member

@mortenta Can you change your craftcms/cms requirement in composer.json to "dev-bugfix/14731-group-params-by-delta-names as 5.0.0" and run composer update, and let us know if that fixes this for you?

@mortenta
Copy link
Author

mortenta commented Apr 2, 2024

@brandonkelly I'm sorry, it doesn't seem to fix the problem.

I changed the composer.json file to:

"require": {
  "convergine/craft-pexels": "1.0.1",
  "craftcms/ckeditor": "4.0.3",
  "craftcms/cms": "dev-bugfix/14731-group-params-by-delta-names as 5.0.0",
  "craftcms/feed-me": "6.x-dev",
  "nystudio107/craft-seomatic": "5.0.0-beta.8",
  "putyourlightson/craft-blitz": "5.0.0-beta.4",
  "vlucas/phpdotenv": "^5.4.0"
},

then run composer update, verified:
Downloading craftcms/cms (dev-bugfix/14731-group-params-by-delta-names 32977ef)

No need to do any php craft xxx/xxx right?

Then did the same procedure once again, just changed the handle and values to be sure nothing was stuck from the last test. Still got the same result.

@mortenta mortenta closed this as completed Apr 2, 2024
@mortenta mortenta reopened this Apr 2, 2024
@brandonkelly
Copy link
Member

Would you mind running composer update once more, and giving it another test?

@mortenta
Copy link
Author

mortenta commented Apr 3, 2024

@brandonkelly I have run composer update and done the same test, unfortunately with the same result.

If you want, I could email you my project files and possibly a screen recording?

@brandonkelly
Copy link
Member

That would be great! Can you send a database backup and Composer files over to support@craftcms.com? And just point out which section you’re seeing this in.

@mortenta
Copy link
Author

mortenta commented Apr 3, 2024

I've sent an email with the dump and files now.

@brandonkelly
Copy link
Member

brandonkelly commented Apr 3, 2024

OK so the issue here is:

  • The Matrix field is in Cards view, so nested Dropdown fields aren’t being rendered as part of the parent entry. So they’re not given a chance to set themselves to the default option value in the UI and mark themselves as changed, until the entry slideout is opened/saved.
  • If you don’t save the nested entries first, the Matrix field doesn’t validate, and prevents the parent entry from saving.

A Matrix field set to card view, with four nested entries that have validation errors.

It’s definitely awkward that to resolve this, you must open the nested entries’ slideouts and then save them without manually changing anything. Not a great UX.

It would also be a little unexpected for Dropdown fields to start setting themselves to the default value whenever they are invalid, rather than registering a validation error.

So I’m not completely sure how we should solve this. We’ll discuss internally.

In the meantime, you can resolve it yourself by running the following command:

php craft resave/entries \
  --field blockCardsContainerCard \
  --type blockCardsContainerCard \
  --set yetanotherdd --to "=bx" --if-empty

@mortenta
Copy link
Author

mortenta commented Apr 6, 2024

Awesome, thanks!

@brandonkelly
Copy link
Member

We discussed this today and decided to keep things mostly as-is, and to document how to use the resave/entries command to set initial values to Dropdown fields when needed.

One thing that needed to be improved is, --if-empty only helps when adding a new Dropdown field to an entry type. If you happen to rename or remove an existing value, you’d be out of luck. So I just added a new --if-invalid option for Craft 4.9 and 5.1, which will set the value if it’s empty (and no empty option is allowed) or it’s set to an invalid option value.

@brandonkelly
Copy link
Member

Craft 4.9 and 5.1 are out with that --if-invalid option.

@DynamiteGoesBoom
Copy link

I've ran these commands:

./craft resave/entries --if-invalid --field contentBuilder
./craft resave/entries --if-invalid --field heroBuilder

But the child entries for each matrix field still has errors when saving the main entry.

I thought I needed the type as well so I tried this ./craft resave/entries --if-invalid --field contentBuilder --type callToAction but I'm still getting the same error. Do I need to run the FeedMe feed again for this fix to work?

@brandonkelly
Copy link
Member

@DynamiteGoesBoom What error are you getting?

@DynamiteGoesBoom
Copy link

No error running the command via the CLI but the entries still show red when saving.

@brandonkelly
Copy link
Member

brandonkelly commented Jul 24, 2024

And the red entries don’t have any sort of error message beside them?

Any chance we can get our hands on your database and Composer files, so we can look into it on our end? If so, please send them to support@craftcms.com

@ryanleichty
Copy link

I just ran into this particular scenario. The image item entries were just created. In order to resolve, this I had to reopen each of them and save them again (or run the commands you listed above). Definitely not a great UX, especially if this were an author running into this.

CleanShot 2024-09-10 at 09 43 52@2x

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

No branches or pull requests

5 participants