Background is all in #1389238: Autocomplete widget improvements. However that issue is in a confused state. The issue was originally a feature request. Subsequently it was re-opened and comments were added to observe the feature was later broken. However the category is still "Feature request" the issue is quite old, the title doesn't reflect the current bug - and so I think the new problem has got lost.

Here's a summary of the status:

  • Auto-complete widget was enhanced to support using user-supplied value when user does not use the auto-complete functionality. This scenario can occur if scripts are disabled, or if you press tab/escape without letting the auto-complete script add it's entity ID in brackets.
  • A subsequent check-in broke this enhancement.
  • It's now worse than if the feature didn't exist at all. If you try to use the feature, the field ends up with no value - even if it is a required field.

There is a patch in #1389238: Autocomplete widget improvements.

Comments

nikita petrov’s picture

AdamPS, I expirienced that problem too. My customer paid me for fixing that problem and I found the way to check whether the user's manual entry have the corresponding entity in our site or not. If there is no appropriate value then I throw a form error 'There is no entity for that input' and ask the user to fill the correct value in that field. Please, correct me if I misunderstand something, the code from the following path is working for me: (I will atach it to the next comment).

nikita petrov’s picture

I'm sorry, how to delete the duplicated comment?

nikita petrov’s picture

I'm sorry, how to delete the duplicated comment?

nikita petrov’s picture

I'm sorry, how to delete the duplicated comment?

nikita petrov’s picture

I'm sorry, how to delete the duplicated comment?

nikita petrov’s picture

Here is the patch for solving my problem. Is it correct?

Status: Needs review » Needs work

The last submitted patch, 6: entityreference-autocomplete-widget-2375957-6.patch, failed testing.

adamps’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new1.03 KB

@Nikita thanks for taking the time to respond. However as I said in my original issue

There is a patch in #1389238: Autocomplete widget improvements.

The next step is to persuade the maintainers to commit the fix. Sorry, I should have set the status of the issue to indicate this. I will also copy the patch across here.

NB this patch is an exact copy of the one by sammys on the related issue. Please send all credit to him. I have only copied it here to help make it clear that this issue is ready for commit.

adamps’s picture

nikita petrov’s picture

Thank you, Adam, sorry for my interferencing here.

Garrett Albright’s picture

Patch in #8 fixes a bug we were experiencing on a client site which sounds a bit different from the OP:

  1. User tries to reference "Foo Bar"
  2. User enters "Foo bar" into the text field (note different casing), but doesn't select one of the autocomplete options
  3. Form is submitted and passes validation, but the entity is saved with the field "empty."

This was because the value that would eventually be returned from this method was the entity type name instead of the entity ID, and that value was later being discarded down the line without the validation failure reaching back up to the entity form.

+1 for RTBC.

13rac1’s picture

+1 for #8. Works well.

ikeigenwijs’s picture

Status: Reviewed & tested by the community » Needs work

Patch in #8 did not fix it for us.

In xdebug did step by step
The function validateAutocompleteInput in EntityReference_SelectionHandler_Generic.class.php is never hit.

  public function validateAutocompleteInput($input, &$element, &$form_state, $form) {

The following validation function is hit in entityreference.module

function entityreference_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
adamps’s picture

Status: Needs work » Reviewed & tested by the community

@ikeigenwijs If validateAutocompleteInput is not hit at all then I would say that you have a different problem from the one in this issue. The patch merely fixes the code within the function, which of course can only help you once you have solved the bug that means the function isn't being called on your site.

It's a while since I've looked at this code, and I'm not an expert - but if I remember correctly, some client side javascript should trigger a call to validateAutocompleteInput as you start to type. Perhaps you have some other javascript file that has a bug in which is preventing other scripts from running? You can check the console output in Firebug on Firefox or whatever equivalent tool you prefer.

entityreference_field_validate I think gets called when you actually submit, so the timing is different.

ikeigenwijs’s picture

thx for the tip I will search in that direction.

mgifford’s picture

So no longer RTBC @ikeigenwijs?

ikeigenwijs’s picture

@mgifford,

AdamPS suggested that my problem of the validation is related to the Ajax.
But i cant find where the call should originate to trigger the validation.

The code of the validation itself looks sound, its just not triggered on my install and cant find out why :s

adamps’s picture

From a quick look

  • entityreference_field_widget_form defines #autocomplete_path
  • The code to handle #autocomplete_path is in form.inc, and it loads autocomplete.js
  • entityreference_menu defines the autocomplete URLs 'entityreference/autocomplete/tags/ or 'entityreference/autocomplete/single/
  • These URLs call entityreference_autocomplete_callback

If you need further help, please post your own issue (or maybe http://drupal.stackexchange.com/) rather than hijacking this issue :-).

damienmckenna’s picture

adamps’s picture

@DamienMcKenna Thanks - you are a busy man with so many modules to look after! NB as per #8 I copied the patch here from the related issue, please send credit to sammys if you remember.

damienmckenna’s picture

@AdamPS: I'm not a comaintainer of ER, just trying to help cheerlead the next release :)

azinck’s picture

#8 works for me, confirming RTBC.

anybody’s picture

+1 for RTBC

dbiscalchin’s picture

#8 RTBC

anrikun’s picture

Status: Reviewed & tested by the community » Closed (duplicate)

This is a duplicate of #1974202: Values not saved when not selected from the autocomplete menu which was created even earlier.
Patch proposed there is almost the same.