l create a content type which has a field of entity_reference, called field_user, the widgets type is autocomplete, target type is user,and I create a views display attached, and set it as required.

when I input existed user's keyword, field_user can search out user and node save is OK.

But when input user's keyword which no user has such keyword, the views behind entity_reference attached have no result return,this is fine,but the wrong keyword which I input in autocomplete widgtes text is still there. as field_user is not empty(staticsfied with required) so when I submit the node it' created suceefully even didn't check the field_user is valid or not.

after the node saved, I found that there is no field_user value show,it's still empty.

why entity_reference didn't clean up input of autocomplete when there is no result return and raise an error when submited?

Files: 
CommentFileSizeAuthor
#20 entityreference-autocomplete-1702172-20.patch1.83 KBakamustang
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es).
[ View ]
#15 validation_views_mode-1702172-15.patch2.45 KBpacproduct
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es).
[ View ]
#13 validation_views_mode-1702172-13.patch2.46 KBpacproduct
PASSED: [[SimpleTest]]: [MySQL] 119 pass(es).
[ View ]
#9 validation_views_mode-1702172-9.patch2.45 KBpacproduct
PASSED: [[SimpleTest]]: [MySQL] 119 pass(es).
[ View ]
#8 validation_views_mode-1702172-8.patch1.73 KBpacproduct
PASSED: [[SimpleTest]]: [MySQL] 119 pass(es).
[ View ]
#7 entityreference_1702172-7_views_autocomplete_validation.patch1.49 KBpacproduct
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch entityreference_1702172-7_views_autocomplete_validation.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Comments

r u serious this is happening with you?? I tried creating this situation but i never found such error..
IF the entity is not found then it will not create that Node..

Same issue here. My required reference field validates when a title is typed in autocomplete, but not selected from dropdown adding the nid.

For example, manually typing in "netflix" validates, even though field should be "Netflix (X)"

End result - the form submits, and the reference field is empty even when required.

Title:save node even input is not valid in autocompleteSaving allowed even when input is not valid in autocomplete results

I can confirm this bug.

I can create a node that has 'asdfasdfgqerbge' for the value of the entityreference field and it still saves.
I can assure you that entity does not exist.

Hi,
I am not able to replicate this issue. Can you please give me some more inputs like related module version and drupal version which you used so that we can replicate?

Thanks.

I just tried on a different site and it works as expected.

I'll check again on the problem site tomorrow and see what's going on.

I'm facing the same issue here (Checked with both versions 7.x-1.0 and 7.x-1.x-dev).

As stated above, the problem arises when the entityreference field is used with the "Views: Filter by an entity reference view" mode.
In my case, I'm referencing nodes.

During my tests, the code that seems to cause the validation to be ineffective is this function (in EntityReference_SelectionHandler_Views.class.php):

  public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
    return NULL;
  }

Seems like this is directly related to #1819618: Incorrect autocomplete validation with views selection mode claiming that this bug has already been fixed fore the "simple" mode here: #1389238: Autocomplete widget improvements. The view mode might need a similar fix as well.

StatusFileSize
new1.49 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch entityreference_1702172-7_views_autocomplete_validation.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

I'm wondering if the same code that was added to the EntityReference_SelectionHandler_Classic.class.php file to fix the "simple" mode wouldn't fit our needs?

I've tried to copy-paste it from EntityReference_SelectionHandler_Classic.class.php to EntityReference_SelectionHandler_Views.class.php, and it's working well in my case. Here is what my EntityReference_SelectionHandler_Views->validateAutocompleteInput() function looks like now:

<?php
 
/**
   * Implements EntityReferenceHandler::validateAutocompleteInput().
   */
 
public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
     
$entities = $this->getReferencableEntities($input, '=', 6);
      if (empty(
$entities)) {
       
// Error if there are no entities available for a required field.
       
form_error($element, t('There are no entities matching "%value"', array('%value' => $input)));
      }
      elseif (
count($entities) > 5) {
       
// Error if there are more than 5 matching entities.
       
form_error($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)"', array(
         
'%value' => $input,
         
'@value' => $input,
         
'@id' => key($entities),
        )));
      }
      elseif (
count($entities) > 1) {
       
// More helpful error if there are only a few matching entities.
       
$multiples = array();
        foreach (
$entities as $id => $name) {
         
$multiples[] = $name . ' (' . $id . ')';
        }
       
form_error($element, t('Multiple entities match this reference; "%multiple"', array('%multiple' => implode('", "', $multiples))));
      }
      else {
       
// Take the one and only matching entity.
       
return key($entities);
      }
  }
?>

What do you think?
(the attached patch just adds the content of the function, as shown above)

Status:Active» Needs work
StatusFileSize
new1.73 KB
PASSED: [[SimpleTest]]: [MySQL] 119 pass(es).
[ View ]

Patch revamped to match the expected format.

StatusFileSize
new2.45 KB
PASSED: [[SimpleTest]]: [MySQL] 119 pass(es).
[ View ]

Actually using the code from the Generic class could not work properly as the View method adds an additional level in the returning array: The bundle type. I've run some additional tests and corrected the patch to take this into account.

The patch in #9 didn't work for me. Saving is still allowed.

I've just tested the following:
I've installed a Drupal 7.19 (standard install) with:
- Entityreference 7.x-1.0+4-dev
- Views & Views UI

I've create 2 basic pages "Basic page 1" and "Basic page 2" to have some initial nodes.

Then I've created an Entityreference view listing my nodes.
I've added an Entityreference field to the Basic page content type:
- using the "Autocomplete" widget
- using the view I've created
- required

When I tried to edit one of my basic pages, here is what happened:
- When I filled the entityreference field with "aetiouoiu", the node was updated, but the entityreference field was left blank.
- When I filled the entityreference field with "Basic", the node was updated, but the entityreference field was left blank.
- When I filled the entityreference field with "Basic page 1", the node was updated, but the entityreference field was left blank.
- When I filled the entityreference field with "Basic page 1 (1)", the node was updated, and the entityreference field was correctly saved.

Then, I've applied my patch #9 to entityreference.

When I tried to edit one of my basic pages, here is what happened:
- When I filled the entityreference field with "aetiouoiu", the node couldn't be saved because the text entered did not match any entity.
- When I filled the entityreference field with "Basic", the node couldn't be saved because the text entered matched several entities. Note: There is a cosmetic issue in this case: The html returned by the view is displayed in the error message.
- When I filled the entityreference field with "Basic page 1", the node was updated, and the entityreference field was correctly saved, referencing my node 1.
- When I filled the entityreference field with "Basic page 1 (1)", the node was updated, and the entityreference field was correctly saved, referencing my node 1.

@castelar, what is different between my tests and yours?

Just tried again, and it works. Even works when the letter case does not match. Thanks!

StatusFileSize
new2.46 KB
PASSED: [[SimpleTest]]: [MySQL] 119 pass(es).
[ View ]

Patch reviewed to fix the cosmetic issue I was talking about in #11.

Status:Needs work» Needs review

Marking as needs review for the test bot.

+++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
@@ -147,7 +147,48 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio
+      $entities = $entities[key($entities)];

Use $entities = current($entities); instead.

StatusFileSize
new2.45 KB
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es).
[ View ]

Patch re-rolled with Dean's improvement.

Bug exists also for node-filters (not views). Tested with Entity Reference 7.x-1.0 on a Drupal 7.23

I've applied patch #15 but the bug still exists.

I have a taxonomy with a field collection field containing an entity reference to a user using the "Views: Filter by an entity reference view" mode.

Could the field collection layer be contributing to the problem?

Issue summary:View changes

I've try the patch, and works so fine, but when you filled with a "0", saves the node with no reference. I tried to fix it, even with rules, with no results

Status:Needs review» Reviewed & tested by the community

Patch works, thanks all

PS: I think field collection can best be handled in a separate issue

Status:Reviewed & tested by the community» Needs review
Related issues:+#1959624: Autocomplete widgets not referencing the single entity result
StatusFileSize
new1.83 KB
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es).
[ View ]

This is actually a bug in EntityReference-SelectionHandler-Generic.class.php as well, which is being fixed over at #1959624: Autocomplete widgets not referencing the single entity result.

So here is a re-roll using the new generic method.

Same issue with entityform having entityreference field, referring to node.
Patch worked but in error message span entities are displaying instead of rendered message(drupal default Bartik theme).

Multiple entities match this reference; " <span class="views-field views-field-title"> <span class="field-content">Emma Roberts</span> </span> (11137)", " <span class="views-field views-field-title"> <span class="field-content">Michael Fassbender</span> </span> (11136)"