I am using Better expose filter auto-submit feature in one of my views filters.When I type something in search field,with the very first letter it starts searching while the user is still typing and throws the result without collecting all keywords.For example If I am searching for India,it will start searching as soon as it receives the First letter of the word 'I' and again on 'N' keyword entry.There needs to be some sort of delay so that user can enter the string which needs to be searched properly.
There is an auto_submit library attached to this module containing the auto_submit.js file in js directory.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Kushal Bansal created an issue. See original summary.

sagesolutions’s picture

Also related to this issue, when typing in the search box and make a typo, hitting backspace doesn't remove a character. Instead it triggers the browser's back button.

This happens because the search results are returned, and you are no longer inside the search box.

C13L0’s picture

Agreed! This needs some sort of a delay. It's nearly impossible to use autosubmit on an exposed combined field.

Kushal Bansal’s picture

Hi @C13L0

The solution for an delay can be submitting a patch for it for the file auto_submit.js in js folder at line no 84

Drupal.debounce(triggerSubmit, 500)($target);

if we can change the Delay to 1500 ,it will improve the way it works.

Will submit a patch for this file.

sagesolutions’s picture

That will work pretty well I think.

I was wondering, should it auto-submit at all when the user is typing in the textfield? I think changing select options or checking checkboxes should auto-submit the form, but maybe not auto-submit while the user is typing. Thoughts?

Kushal Bansal’s picture

The code for this is written on behalf of on keyup events.So it is working like that!

Kushal Bansal’s picture

FileSize
3.26 KB

I am submitting this file which contains the updated auto_submit.js file to fix the time delay issue .

Kushal Bansal’s picture

          Drupal.debounce(triggerSubmit, 1500)($target);

Here The delay has been increased from 500 to 1500

joake’s picture

Another option is to set the data-bef-auto-submit-exclude="1" on the input field in question. This can be done in hook_form_alter, this will prevent said field from triggering auto update entirely.

Coufu’s picture

#9 worked for me. Thanks joake.

luzitano’s picture

#9 also worked for me. In fact, I just went ahead and made all text inputs in exposed filters across all views have this attribute set.

Alireza.Tabatabaeian’s picture

Tried to test #9 so I implemented the below code:

function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if($form_id == "views_exposed_form") {
    $form['title']['data-bef-auto-submit-exclude'] = 1;
  }
}

well, I know I might be implementing it wrong so I wanted to ask how can I set this option on my form element?

@luzitano can you share your code if you don't mind?

Kleve’s picture

This is how I implemented it.

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'views_exposed_form' && $form_state->getStorage('view')['view']->id() == 'VIEW_NAME') {
    // Disable auto submit for search text field.
    if (isset($form['search'])) {
      $form['search']['#attributes']['data-bef-auto-submit-exclude'] = '1';
    }
  }
}
interdruper’s picture

Yep, #9 approach works. Note that this is also required for autocomplete filters, like i.e. $form['uid'].

matsbla’s picture

Title: Autosubmit issue in search filter by Expose form » Text fields are auto-submitted before user have finished typing keywords

I don't think #9 is a good solution, it simply turns off the autosubmit for text fields, but what if you need autosubmit when the user have finished typing?

#8 gave a slight improvement, but doesn't makes it possible to write longer words. I think we need a debounce that check if user where still typing e.g. the last second, and continue to wait autosubmit until user have not typed anything for the last second.

trebormc’s picture

FileSize
1.2 KB

I created a patch to detect when you stop typing. There is a wait of 1s to make sure that it is not because the user types slow.

oxyc’s picture

The issue is that debounce is used incorrectly. This is not pretty but it fixes it.

tostinni’s picture

Since #2880307: Allow Textfields to be excluded from AutoSubmit, there's the option in the UI to disable ajax on textfields.

Regarding the patch, I think adding a delay is a good idea but it should be configurable, so here is a patch that adds a setting for this.
It's only visible if "Autosubmit" is checked and "Exclude Textfield" isn't.

fotidim’s picture

It would also make sense to search when there are at least 3 characters in the field. This can also be configurable.

BrightBold’s picture

Status: Needs work » Needs review

@tostinni (and previous patch contributors) You are lifesavers! The patch in #18 fixed my problem. We are using an autocomplete field so needed a 1500ms delay, so I really appreciated the configurable setting.

From my perspective this is RTBC but I've put it to Needs review so that someone else can confirm.

sonvir249’s picture

Status: Needs review » Reviewed & tested by the community

The patch in #18 fixed my problem. Thank you @tostinni, for the patch

matsbla’s picture

A similar solution is being worked on for ctools

Sharique’s picture

+1 for RTBC, It fixed the issue for me also.

rlhawk’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
4.29 KB
620 bytes

Whenever a new setting is added, the configuration schema needs to be updated to include it. Here a new patch (and interdiff) that updates the schema.

jeremyr’s picture

Confirming that #24 works.

brayfe’s picture

Have also confirmed that the patch #24 works with BEF 8.x-3.x-dev on Drupal 8.6.14.

I'd be interested to hear the use case for having auto_submit on an exposed Textfield in the first place. It seems strange, especially if there is already a "Submit/Apply" button. It also negates the natural behavior to push the "enter" key to submit. Seems like what people want is an ajax form for this textfield vs auto_submit.

brayfe’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -autosubmit expose form better-expose-form views filter search +autosubmit expose form better-expose-form views filter search seattle2019

Changing this to RTBC as it allows the configuration of the autosubmit delay. Up for consideration, for anyone who comes across this post in the future, is the suggestion in #9 (don't autosubmit text fields).

  • rlhawk committed 3e6a1e6 on 8.x-3.x authored by tostinni
    Issue #2921744 by tostinni, oxyc, trebormc, Kushal Bansal, rlhawk: Text...
rlhawk’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, all.

Status: Fixed » Closed (fixed)

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

turpentyne’s picture

Forgive a beginner question on a closed item, but...

Would the patch in #24 above, work on 8.x-3.0-alpha6 ?

I don't see BEF 8.x-3.x-dev available for install. I tried installing better_exposed_filters 8.x-4.x-dev, but it broke the site, so I reverted to alpha6, since it seemed more stable on my site, for the moment.

alberto56’s picture

@turpentyne yes it will. You will notice that the patch was committed on April 12, and the alpha6 version dates from before that. So, yes, it will apply.

StijnStroobants’s picture

Re-rolled the patch against the 8.x-4.x- branch.

StijnStroobants’s picture

Forgot to attach the drupalSettings. New patch uploaded.

StijnStroobants’s picture

StijnStroobants’s picture

I would like to re-open this issue for review as this functionality was not implemented in 8.x-4.x

StijnStroobants’s picture

Version: 8.x-3.x-dev » 8.x-4.x-dev
StijnStroobants’s picture

Opened a new issue for the missing functionality of in the 8.x-4.x branch: https://www.drupal.org/project/better_exposed_filters/issues/3098809