Ajax not working with address

Created on 6 December 2021, over 2 years ago
Updated 30 November 2023, 7 months ago

Problem/Motivation

#ajax behavior attached via hook_webform_alter not working with address.

Steps to reproduce

Create form with simple address.
Create a second field (for test) using text or email
Create third field to hide/show
Create module with implmentation of hook_webform_alter and attach ajax call back

$form['elements']['country_code']['#ajax'] = [
    'callback' => 'ajax_callback',
    'wrapper' => 'opt-in-wrapper',
    'disable-refocus' => FALSE,
    'event' => 'change',
  ];

Call back is not called.
A similar issue was reported a while back here with no resolution

Change the Ajax to any other field and it works as expected

πŸ’¬ Support request
Status

Fixed

Version

1.9

Component

Miscellaneous

Created by

πŸ‡ΊπŸ‡ΈUnited States TMWagner

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Same issue here. Need to ajaxify address field the zipcode field, tried after_build, #ajax, #process callbacks, didnt worked. But if we give #ajax callback to main address field it works but still only the ajax progress bar comes in that scenario.

  • Status changed to Closed: works as designed 8 months ago
  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Tried address 1.x with webforms 6.2 and was able to reproduce the issue, however I don't think that's issue.
    During hook_webform_alter or hook_webform_element_alter (from 6.2 version) there are no inner elements for address field, only main #type address field which only serves as wrapper for address textfields and selects, so attaching ajax here won't do anything.
    After built also was not successful and I could not figure out why, perhaps because address element itself uses ajax and attaches it in process function.
    Best way to add ajax there is to add hook_element_info_alter and add custom #process to address element, there in process callback it's possible to add ajax to country_code, postal_code and all inner elements, but please beware of existing ajax on country_code. If it is necessary to add something there you can change ajax callback and do what is needed and return Drupal\address\Element\Address::ajaxRefresh() or return array of ajax commands.
    Anyway, I don't see any point in changing module code since there's a way to add ajax there as I described above. If there is still something we can do about elements in module to make ajax work better, please reopen with suggestions.

  • Status changed to RTBC 8 months ago
  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Hi TMWagner, please check if solution from comment above works for you.

    Hi maintainers, please close/fix the issue if you agree on the comment above and we don't need to apply ajax from address element to any inner

  • Status changed to Fixed 7 months ago
  • πŸ‡·πŸ‡ΈSerbia bojanz

    Don't see what else we can do for now, so closing this. Thanks, sickness29.

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

Production build 0.69.0 2024