Problem/Motivation
When you have an entity_autocomplete form element and the cardinality is set to 1, the input textbox should only accept a single value. If the cardinality is set to > 1 then >1 textboxes are presented to the user to enter each value. But the same behaviour occurs for each of the textboxes. It is possible to enter multiple comma-separated values in each one. And only the last value is saved.
Steps to reproduce
- Install a vanilla Drupal site version ^11.4 or 12-dev
- Create a custom content type with one field e.g. an integer field
- Create several nodes of the custom content type.
- Add an entity reference field to the custom content type
- Set the cardinality of the field to 1
- Edit one of the nodes. Enter the title of one of the other nodes then a comma, then the title of another node.
- Save the node. Note that only the value of the last entered referenced node is saved.
- Change the cardinality of the field to 2.
- Edit a node. Enter multiple titles of other nodes, comma-separated, in the 2nd entity-autocomplete input textbox
- Save the node. Note that only the last of the entered values is saved.
Proposed resolution
- Do not autocomplete past the first item.
- Add a form error if multiple values were entered.
Remaining tasks
User interface changes
Adding more than one comma-separated value in an entity-autocomplete form field will trigger a form error. No value will be saved until there is only one value entered in the textbox.
Introduced terminology
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
N/A
Original report by [username]
N/A
| Comment | File | Size | Author |
|---|---|---|---|
| #44 | Entity-autocomplete-element-single-cardinality-error-on-attempt-to-save-multi-values.png | 114.38 KB | ironnuts |
| #36 | 2692289-36.patch | 1.04 KB | marckwee |
| #32 | 2692289-30-9.3.0.patch | 1.14 KB | basvanderheijden |
| #26 | 2692289-25-9.3.0.patch | 1.16 KB | naveenvalecha |
| #20 | interdiff_16-20.txt | 1.05 KB | neslee canil pinto |
Issue fork drupal-2692289
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
kevin.dutra commentedI'm going to leave this at normal priority for the time being. A case could be made for data loss, since it's dropping the other values without any warning, but I don't think that this form element is really being used on it's own much in core. I'll happily defer to someone else if they think priority should be higher.
Comment #9
oknateOur QA team reported this bug. I've attached a video I created using the EntityReferenceAutocompleteWidgetTest.php using Drupal 8.8 alpha.
This is still an issue. There should be some sort of javascript preventing more than the allowed cardinality from being entered. Or perhaps validation on the backend that returns an error. Nothing terrible happens, but it allows you do something odd.
Comment #10
danielvezaAssigning to myself to have a dig in over the upcoming weekend. If others want to jump ahead feel free.
Comment #11
danielvezaEDIT - Ah sorry ignore this patch - Had a bunch of local changes I was playing with for a talk.
Comment #12
danielvezaLooking at the code it looks like this is desired behaviour - But I think I agree that it should fail validation instead of just grab the last value. Thats too confusing for editors IMO.
I think we need a test for this too if everyone agrees it should work this way.
Comment #13
larowlanI think this should use
\Drupal\Component\Utility\Tags::explodeinstead of just regular explodewe can return here and avoid the else to reduce complexity
I agree - this sounds like a genuine bug - we should be able to reproduce it in a test
Comment #14
krzysztof domańskiComment #15
glenndw commentedI have edit the patch. When you have references for autocomplete that contain a comma (like node title) previous patch will not allow it.
When Drupal detects a comma in a reference it will put quotes around the hole thing. So now we have a regex that will see if a comma is in the value that is outside of quotes.
Example
This wil be oké
value -> "title part1,2,3 (node_ID)"
This is not
value -> "title part1,2,3 (node_ID)", "title part 4,5,6 (node_ID)"
In previous patch
value -> "title part1,2,3 (node_ID)" Trows Error message
Comment #16
hardik_patel_12 commentedReapplying patch.
Comment #17
hardik_patel_12 commentedComment #18
Lal_why are we commenting this ?
Comment #19
Lal_Comment #20
neslee canil pintoComment #22
karthikeyan-manivasagam commentedHi,
Thank you all, after applying the patch, When trying to add multiple values in comma separated instead of showing validation error it throws fatal error, on Drupal core 8.9.20
InvalidArgumentException: Placeholders must have a trailing [] if they are to be expanded with an array of values. in Drupal\Core\Database\Connection->expandArguments() (line 744 of /Applications/MAMP/htdocs/core/lib/Drupal/Core/Database/Connection.php)Thanks
Karthikeyan
Comment #23
karthikeyan-manivasagam commentedComment #26
naveenvalechaHere's the patch for 9.3.0
Comment #27
danflanagan8This is an interesting issue because, as pointed out in #12, the code spells out this behavior:
The patches above address item #2 in the IS, which might not even be classified as a bug since it is the behavior specified in code. I think we all agree the current situation is bad UX though. But if we add this validation though we clearly must update the comment too.
I also think that the regex in #26 should be removed in favor of calling
Tags::explodeand then callingcounton the result. We might as well re-use the existing code that does this.As far as item #1 in the IS, there's nothing in any of the patches that addresses that. That would probably involve some changes to the
core/drupal.autocompletelibrary.I would think that we could split those up into separate issues if we wanted. My guess is that messing with
core/drupal.autocompletecould get really nasty.Comment #29
liquidcms commentedJust tossing in my test results and comments:
- tested with core 9.2.16 and patch from #20 (#26 doesnt apply)
without patch:
- only the LAST item on the list is passed to my form submit handler
with patch:
- if i enter 2 items i get a form validation error
this is independent of setting #tags
IMHO, the proper functionality here is:
- if something is set on the form (#tags seems like an odd attr to define this), nothing should autocomplete past the 1st item - i think currently missing
- if user just enters values manually without aide of autocomplete; then validation error (as the patch does now)
- if single attr is not set; then all items should be passed to form submit - not working now
are these the goals here?
Comment #32
basvanderheijden commentedThe patch in #25 does not work because of a malformed RegEx. My patch uses Tags::explode instead.
Comment #34
sascha_meissner+1 Just stumbled into this ... IMHO it makes more sense to prevent completing multiple values client side in the first place. Just adding serverside validation is good but will just raise questionmarks in the opposite direction.
EDIT: looked a bit further and as the autocomplete js code will be replaced soon https://www.drupal.org/project/drupal/issues/3466088 my comment doesnt worth mention here
Comment #36
marckwee commentedHi,
I made a patch that is working with D11.
Comment #39
phthlaap commentedI added tests to reproduce the issue, and applied patch for it.
Comment #40
smustgrave commentedCould use a summary update to include the steps to rerpoduce
Comment #41
ironnuts commentedComment #42
ironnuts commentedComment #43
ironnuts commentedManually tested MR !16064 on Drupal 12.0-dev.
When attempting to save a node with multiple values entered in a single cardinality entity autocomplete element, save fails with an error message.
Comment #44
ironnuts commentedComment #45
ironnuts commentedComment #46
ironnuts commentedThere is now a failing test in place. The pipeline is otherwise all green. Manual testing seems acceptable.
Comment #47
quietone commentedEveryone, please remember to update the issue tags.
This is changing the user interface, so adding tag, and there should be before and after screenshots available from the issue summary.
Comment #48
smustgrave commented@quietone left some good comments on the MR. If UX team is needed lets update the IS with screenshots to help them.