By papagrande on
I want to add AJAX to an autocomplete field on a form that fills in two other fields with data based on what the user chooses in the autocomplete field. The AJAX was working when the first field was a select list, but when I changed it to an autocomplete field, the event trigger no works. What do you I need to do beyond attaching the AJAX callback to the autocomplete field? Any suggestions or examples would be very welcome.
Comments
Perhaps this could be of use
Perhaps this could be of use to you
http://drupal.org/project/examples
Entity Reference
If the autocomplete field is an Entity Reference field, the solution that worked for me is to put the ajax callback directly to the field "target_id".
e.g.:
$form['field_yourfield'][LANGUAGE_NONE][0]['target_id']['#ajax'] = array()This is the magic!
Yup, it's just an issue of finding the right place to attach the #ajax attribute. Doing this for my autocomplete field did get my ajax callback to fire.
http://blog.marceisaacson.com
Thanks @Raumfisch! Adding
Thanks @Raumfisch! Adding the #ajax callback on the target id worked for me.
where can I find the nid, or target_id returned by autocomplete?
I'm working with a node reference, so I added #ajax this way
this works, I'm using ajax to modify other field, but I can't find the actual nid (or target_id) returned after autocomplete
when I look at $form_state['input']['field_myfield']['und] or $form_state['values']['field_myfield']['und'] I just found the text I entered, not the actual nid returned by autocomplete
I've also looked at $form_state['triggering_element']['#value'] but also, there's just the text I entered for search with autocomplete
so where can I find the nid, or target_id returned by autocomplete ?, or even the full fieldtext value , not just what I've entered ?
please help, thanks
Any thoughts on how to do
Any thoughts on how to do this for D8? Adding the callback directly to the form works, but it doesn't include the selected value in the callback, only the user entered text.
Figured it out- use the event
Figured it out- use the event "autocompletechange"
http://api.jqueryui.com/autocomplete/#event-close
Could you provide more details?
You mentioned "adding the callback directly to the form" works. Do you mean like $form['field_name']['widget'][0]['target_id']['#ajax'] = array(...)? In the callback I use function xyz_callback(&$form, \Drupal\Core\Form\FormStateInterface &$form_state) {...} but nothing is happening...
Eric
I actually had to change the
I actually had to change the event as
autcompletechangewasn't giving me the selected value.autocompleteclosedoes, however. Here is my example form element-Is that helpful?
I was able to use $form[
I was able to use
$form['field_name']['widget'][0]['target_id']['#ajax'] = array(...);to assign and make the call to the callback function.Here is an answer for D7 and
Here is an answer for D7 and D8: http://drupal.stackexchange.com/a/228150/2272
When i use this with an
When i use this with an entity reference it does not work for me. Maybe some of u can find my mistake (the ajax of the select field is working fine):
my entity field is build by this way:
thanks for helping me
best regards
Andy
autocompleteselect
@keithm, from your StackExchange answer:
I noticed this too. Any idea why?
I'd prefer to be able to use autocompleteselect than autocompleteclose, since the close event can fire when the option list is empty. (To see this, start typing letters from some items in the list, then type wrong letters so that there are no longer any options. The close event fires here, which is undesirable in my case.)