Field data lost after form (re)submission because form elements are missing

Created on 22 July 2022, over 3 years ago
Updated 17 October 2025, 17 days ago

Problem/Motivation

The address format (from commerceguys/addressing) determines what form elements (street, postal code, etc) are shown for an Address field in a form. When form elements are not shown, it seems their data is not saved on form submission. Trying to override the address format by setting the elements to 'Required' doesn't work; the predefined format in /vendor/commerceguys/addressing/src/AddressFormat/AddressFormatRepository.php seems to take precedence.
I would expect that marking the element as required in the Address field configuration, would imply that the element is visible on the form? The helper text says: " Use field overrides to override the country-specific address format, forcing specific properties to always be hidden, optional, or required. "

Steps to reproduce

In practice: I have a I have an Address field that gets its elements set by the Geocoder module, based on reverse geocoding a set of coordinates. Langcode is NL, for addresses in the Netherlands, which does not include administrative_area (Province) in the address format. This works fine the first time the field is populated after reverse geocoding, all available elements are populated. However, when the node is saved after editing (and thus not populated by geocoding), the administrative_area data is lost. This seems to be because the form element for administrative_area is never shown, even though it's marked as required. I need the administrative_area data, even though it's not part of the standard NL address format.
If you manually edit the address format in AddressFormatRepository.php to include administrative_area for NL, the form element appears and the value is saved on form submission. (This hack fixes my problem for now.)

Proposed resolution

  • If an element isn't shown on the form, don't overwrite it's data on form submission?
  • Alternatively, allow field override setting to really override the address format?

Remaining tasks

Clear up whether the 'field override' setting actually takes precedence over the address format set in AddressFormatRepository.php. If it should, then there seems to be a bug now? In Address.php, addressElements() checks for required fields, but only seems to use it to mark those fields as 'required' for styling purposes in the form, doesn't determine whether the fields get included at all.

🐛 Bug report
Status

Active

Version

1.10

Component

Code

Created by

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.

No activities found.

Production build 0.71.5 2024