2

I have a project that I need to display some help text to the user if an autocomplete text comes back empty.

Right now, the autocomplete is on a custom form, and I'm using an autocomplete like:

function autocomplete($string) {
  $result = autocomplete_lookup($string);
  $matches = array();
  // Save the query.
  foreach ($result as $row) {
    // Perfom my logic rows here.
  }

  drupal_json_output($matches);
}

Is there a way I can run a check at some point in the process to display a pop up or something if the autocomplete doesn't return anything?

4
  • Are you looking for hook_form_validate() and form_set_error()? Or #element_validate?
    – leymannx
    Commented Sep 20, 2015 at 21:01
  • Or #states? Could you maybe provide more information (code) about the actual form?
    – leymannx
    Commented Sep 20, 2015 at 21:08
  • 1
    Yeah, hook_form_validate wouldn't do it.
    – Jance
    Commented Dec 23, 2016 at 18:57
  • 1
    I'll have to take a closer look at states. I'm not sure hook_form_validate would work, as autocomplete isn't a form, and it needs to execute before the form is submitted.
    – Jance
    Commented Dec 23, 2016 at 19:02

2 Answers 2

1

Not sure if it would help or not, but if your looking for trying to tell the user nothing exists in the database, you can always add logic to the autocomplete that will return something to the field that indicates no results are found.

2
  • 1
    That would be one way to do it I suppose.
    – Jance
    Commented Dec 23, 2016 at 18:56
  • 1
    Although, it would still be better to have a trigger of some sort.
    – Jance
    Commented Dec 23, 2016 at 18:57
0

You can consider using select2 module. It can take charge of your autocomplete field and will show a message something as 'No Result Found' or similar, but not any pop-up.

1
  • 1
    Is there anyway I can do it without using a contributed module? Like a js event listener?
    – Jance
    Commented Sep 18, 2015 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.