I tested both 7.x-1.2 and 7.x-1.x-dev, same behavior.
When we reference one or several entities, delete them in the meantime, and then save the form, entityreference ids are saved and the field validation doesn't throw any errors.
I'm using the following but it can be reproduced with wide range of settings:
- Create an entityreference field on article nodes (e.g.: target type: taxonomy term, widget: select list)
- Go to the form to create an article /node/add/article
- Fill in the required fields and reference an existing entity
- Open a new tab and delete the entity you just referenced
- Go back to the article form and save
- The article is saved and the referenced id as well. No errors thrown.
There is definitely some logic issue in entityreference_field_validate():
If we reference only unexisting entities, no error is thrown.
If we reference at least one existing and 1 or several unexisting entities, we get the "The referenced entity (@type: @id) is invalid." error as expected.
Looking at entityreference_field_validate() it seems removing the if (!empty($valid_ids))
could fix the issue.
Comment | File | Size | Author |
---|---|---|---|
#2 | fix_entityreference_field_validate_nonexisting_entities-2854471-2.patch | 1.36 KB | herved |
|
Comments
Comment #2
herved CreditAttribution: herved commentedThe
if (!empty($valid_ids))
was added recently by #1753774: Multiple warnings when using entity reference field filtered by a view using contextual filters, inside a field collection.As long as EntityReferenceHandler::validateReferencableEntities() returns an array there shouldn't be any issues/notices. Which is the case for the EntityReference_SelectionHandler_Views class now.
So I believe the "if" can be removed safely but here's a patch which also checks for a non-empty result from validateReferencableEntities().
Comment #3
herved CreditAttribution: herved commentedComment #4
MustangGB CreditAttribution: MustangGB commentedIs this fixed by #1702172: Saving allowed even when input is not valid in autocomplete results?
Comment #5
anrikun CreditAttribution: anrikun commented@MustangGB No it isn't.
Comment #6
anrikun CreditAttribution: anrikun commentedRaising to Major as this is a validation issue.
Comment #7
drumm#2516716: Entity selection target bundle settings are not enforced looks like the same issue, and that patch has a test.
Comment #8
minorOffense CreditAttribution: minorOffense at Coldfront Labs Inc. commentedClosing as duplicate. Feel free to integrate changes into the other patch if need be (and update tests to get it merged).