When I activate ajax for my views, and enable term autocomplete exposed filter, it looks like that form submit happens too early, when I select some option from autocomplete and hit enter. So, if I write "Пу" and the autocomplete suggests "Пушкин" and I try to select it using my keyboard arrows and enter, the form is submitted with "Пу" and throws validation error. You can reproduce it here:
http://sfera.apcards.ru/catalog
(try to type "Пу" in "Автор" field)

screenshot: http://pix.am/FOGz/

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Status: Active » Postponed (maintainer needs more info)

Can you rebuild this view on a normal english drupal installation so we could try to reproduce the bug and based on this fix the bug?

Everytime an issue is easy to reproduce the bug get's fixed much faster :)

restyler’s picture

http://sfera.apcards.ru/test

Start to type "Ste" in "Author" and when you see "stephen king" select it using keyboard arrows, and press enter.

dawehner’s picture

Can you also please export the view and attach it here? Thanks!

restyler’s picture


$view = new view;
$view->name = 'test';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'test';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'test';
$handler->display->display_options['use_ajax'] = TRUE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'node';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['title']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = 0;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = 0;
$handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['title']['alter']['trim'] = 0;
$handler->display->display_options['fields']['title']['alter']['html'] = 0;
$handler->display->display_options['fields']['title']['hide_empty'] = 0;
$handler->display->display_options['fields']['title']['empty_zero'] = 0;
$handler->display->display_options['fields']['title']['link_to_node'] = 1;
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 0;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'book' => 'book',
  'product' => 'product',
);
/* Filter criterion: Content: Информация об авторе (field_author) */
$handler->display->display_options['filters']['field_author_tid']['id'] = 'field_author_tid';
$handler->display->display_options['filters']['field_author_tid']['table'] = 'field_data_field_author';
$handler->display->display_options['filters']['field_author_tid']['field'] = 'field_author_tid';
$handler->display->display_options['filters']['field_author_tid']['value'] = '';
$handler->display->display_options['filters']['field_author_tid']['exposed'] = TRUE;
$handler->display->display_options['filters']['field_author_tid']['expose']['operator_id'] = 'field_author_tid_op';
$handler->display->display_options['filters']['field_author_tid']['expose']['label'] = 'Author';
$handler->display->display_options['filters']['field_author_tid']['expose']['operator'] = 'field_author_tid_op';
$handler->display->display_options['filters']['field_author_tid']['expose']['identifier'] = 'field_author_tid';
$handler->display->display_options['filters']['field_author_tid']['reduce_duplicates'] = 0;
$handler->display->display_options['filters']['field_author_tid']['vocabulary'] = 'authors';
$handler->display->display_options['filters']['field_author_tid']['error_message'] = 1;

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'test';

dawehner’s picture


$handler->display->display_options['filters']['field_author_tid']['id'] = 'field_author_tid';
$handler->display->display_options['filters']['field_author_tid']['table'] = 'field_data_field_author';
$handler->display->display_options['filters']['field_author_tid']['field'] = 'field_author_tid';
$handler->display->display_options['filters']['field_author_tid']['value'] = '';
$handler->display->display_options['filters']['field_author_tid']['exposed'] = TRUE;
$handler->display->display_options['filters']['field_author_tid']['expose']['operator_id'] = 'field_author_tid_op';
$handler->display->display_options['filters']['field_author_tid']['expose']['label'] = 'Author';
$handler->display->display_options['filters']['field_author_tid']['expose']['operator'] = 'field_author_tid_op';
$handler->display->display_options['filters']['field_author_tid']['expose']['identifier'] = 'field_author_tid';
$handler->display->display_options['filters']['field_author_tid']['reduce_duplicates'] = 0;
$handler->display->display_options['filters']['field_author_tid']['vocabulary'] = 'authors';
$handler->display->display_options['filters']['field_author_tid']['error_message'] = 1;

The problem here is that you would have to create the fields manually and make some mistakes there as well.
Additonal this doesn't improve the motiviation to focus on this issue.

restyler’s picture

I don't get your comment, sorry.
can you please re-phrase?

restyler’s picture

Title: Taxonomy autocomplete + ajax view = error » Views exposed autocomplete + ajax view = form submit happens too early
restyler’s picture

just reproduced the bug again in different situation: textfield field in node, text exposed filter + autocomplete added to the textfield by form_alter hook.

dawehner’s picture

So the problem is probably that the focus is not on the autocomplete but on the actual submit element.

The prober fix would be perhaps that drupal autocomplete set's the focus manually

esmerel’s picture

Status: Postponed (maintainer needs more info) » Active
ichionid’s picture

Hey I have the similar issue! Any update or workaround to it?

reeller_sk’s picture

Same issue here, I tried to add autocomplete to 'tlp_full_name_value' exposed filter in hook_form_views_exposed_form_alter, autocomplete works ok, but when I select a value from autocomplete popup and hit enter, the form is automatically submitted and the selected value isn't used. After ajax load in filter is not selected value but original value. For example I write 'adm' and autocomplete suggest in popup 'Administrator' I select this value and hit enter, after ajax load the value in filter is automatically changed back to 'adm'. I think problem is that the submit is processed before set of the selected value in the popup.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function tl_user_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] == 'views-exposed-form-partner-admin-block-partnership-requests' ||
      $form['#id'] == 'views-exposed-form-partner-admin-block-partner-staff-requests' ||
      $form['#id'] == 'views-exposed-form-partner-admin-page') {
    $form['tlp_full_name_value']['#autocomplete_path'] = 'ajax/autocomplete/name';
  }
}

Exported view code: http://pastebin.com/Tx2Esr9k

reeller_sk’s picture

Version: 7.x-3.x-dev » 7.x-3.3
Component: taxonomy data » Views Form
pcrats33’s picture

Issue summary: View changes

There is a cTools patch that fixes this: https://www.drupal.org/node/1708438

Mitch Boulter’s picture

Unfortunately came across this issue on a project I'm currently working on. The solution I found was to call Drupal.autocompleteSubmit (in autocomplete.js) within Drupal.ajax.prototype.beforeSerialize (in ajax.js) in order to populate the textbox BEFORE the form is serialized. This solved the issue for me, but doesn't feel like an ideal solution. If anyone has a better suggestion I'm all ears.

akz’s picture

@pcrats33: Similar problem and nice patch, but it does not fix this one.

@Drif.io is correct; the problem is a missing trigger of Drupal.autocompleteSubmit when ajax is enabled in a view. Well, autocomplete.js, binds this handler to the form submit event:

$($input[0].form).submit(Drupal.autocompleteSubmit);

However, when ajax is enabled for a view, this event is not fired, and Drupal.autocompleteSubmit should be fired for form input/button with type 'submit' click events. So, this bug can either be fixed from the autocomplete perspective (where the autocompleteSubmit should be also bound to the submit button/input click event), or from the views perspective (where form submit event should be triggered upon ajax submission). I am not sure which is the most proper solution, though, hence not providing a patch or altering the Project value of this issue.

I solved my problem, via a custom js file of my own that only does the following:

Drupal.behaviors.autocompletefix = {
		attach: function (context, settings) {
			$("form[id^=[my-custom-form-id]").find("[type=submit]").click(Drupal.autocompleteSubmit);
		}
	};

The tricky part, is that this js file should be included (therefore executed) after 'misc/autocomplete.js' and before 'views/js/*.js' files. I managed to do that by implementing hook_js_alter; I added a new entry for my custom js file with the same group and scope as 'misc/autocomplete.js' and a slightly bigger weight.

This is definitely not the ideal solution, but it feels better that overriding Drupal.ajax.prototype.beforeSerialize

IRuslan’s picture

I decided to move my version of solution into the separate ticket for Core — #2785283: Enter key press in autocomplete results lead to ajax form submission.
Maybe will be useful for someone.

dsnopek’s picture

I can reproduce this issue too. An easy way to see it is to make a view of nodes and an exposed filter on "Author uid". If AJAX is disabled and you type "ad" and arrow key down to "admin" and press enter, the autocomplete simple injects "admin" and doesn't submit. But if AJAX is enabled, it will put "ad" into the text field and submit the form.

dsnopek’s picture

The CTools patch referred to in #14 doesn't actually fix this for me :-/

dsnopek’s picture

The core patch in #17 does work in my testing! However, getting that included in core will be tough. :-/ If there's a way to fix that on the Views end, it'd be better because it'd be easier to get it included in core

dsnopek’s picture

Status: Active » Needs review
FileSize
565 bytes

Here's a patch that fixes this from Views and works in my limited testing

dsnopek’s picture

And for those who are on Views 3.18 here's a re-rolled patch against the last release, since the current -dev changes how the code in the patched file is indented.

However, don't review/merge this patch! Review/merge the one on #21!

joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community

reviewed code, looks good.

nickonom’s picture

On Views 7.x-3.18 the patch has given:

patch < views-ajax-autocomplete-1264794-21.patch 
can't find file to patch at input line 5
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/js/ajax_view.js b/js/ajax_view.js
|index 51fb806..c3e5bb9 100644
|--- a/js/ajax_view.js
|+++ b/js/ajax_view.js
--------------------------
File to patch: js/ajax_view.js
patching file js/ajax_view.js
Hunk #1 FAILED at 90.
1 out of 1 hunk FAILED -- saving rejects to file js/ajax_view.js.rej

Also after manually applying the patch it didn't fix the problem for me, so probably my use case is different.

joseph.olstad’s picture

@nickonom try this instead:
download/copy the patch file into the views folder and then:
patch -p1 --dry-run < views-ajax-autocomplete-1264794-21.patch

if the --dry-run works (test applying the patch)
then without the --dry-run flag out and actually apply the patch as follows:
patch -p1 < views-ajax-autocomplete-1264794-21.patch

dsnopek’s picture

@nickonom was trying with Views 3.18, for which there is a different patch on #22! That one should apply cleanly.

  • DamienMcKenna committed 325f6be on 7.x-3.x authored by dsnopek
    Issue #1264794 by dsnopek: Views exposed autocomplete + ajax view = form...
DamienMcKenna’s picture

Committed. Thanks all.

joseph.olstad’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

semanthis’s picture

Since that change https://www.drupal.org/project/search_api_autocomplete does not work anymore. I have not fully understood why. Maybe someone can give me a hint how to fix.

joseph.olstad’s picture

@semanthis to revert the commit locally, run the above patch in reverse (using the -R flag)

cd sites/all/modules/contrib/views
wget https://www.drupal.org/files/issues/views-ajax-autocomplete-1264794-21-3.18.patch
patch -p1 -R < views-ajax-autocomplete-1264794-21-3.18.patch

or if you don't have wget you can use curl like this

cd sites/all/modules/contrib/views
curl https://www.drupal.org/files/issues/views-ajax-autocomplete-1264794-21-3.18.patch > views-ajax-autocomplete-1264794-21-3.18.patch
patch -p1 -R < views-ajax-autocomplete-1264794-21-3.18.patch
semanthis’s picture

Thank you, joseph.olstad that's what I did. I wrote a ticket for Search API autocomplete, the solution has to happen there. I just have not time at the moment to digg too deep into the problem, so I just reverted the commit.

joseph.olstad’s picture

joseph.olstad’s picture

Solthun’s picture

I know this issue is merged and closed, but I want to leave a warning here for anyone trying to override how autocomplete selects or submit work.
Please check https://www.drupal.org/project/drupal/issues/309088#comment-12790476