There are a couple of problems when using this field with more than one value.

* If the field is set to required and only one country is selected in the field settings you do not have a country selector and there's no way of removing one field value.
* If the field is set to required and also one of the address fields is also required (e.g. city) then you cannot save the content, because every time you try to edit the content again, there will be another field value added which cannot be removed, but since one of the sub-fields is required, you are stuck.

* If the field is not required or you specified more than one country in the field settings, it is possible to remove one field value. However that is not very straight forward: only after trial and error I found out that you need to set the country to none but when you do that there is a little select where you do not know what to do with it (looks like a weight selector).

See attached images

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pixelmord created an issue. See original summary.

pixelmord’s picture

Issue summary: View changes
bojanz’s picture

Thanks for documenting this. We haven't really optimized for this use case, since it's hard to distinguish multiple addresses semantically (that's why we do some kind of entity above, that can tell us what the purpose of the address is). I'll make sure to address it in the upcoming months.

tamarpe’s picture

Same issue here. On update/creating nodes, the country is set and the node can't be saved/updated because of the required fields..

frederickjh’s picture

Hi All!
I ran into this too. The fix for having to to set the empty field to none when the node is edited is: to go into Manage Form Display then find your Address field and set the country to none. That way when a user edits the node they will not need to go and change the country to none to save.

imclean’s picture

Obviously this is still an issue. It can make sense to lock a user to a specific country and make at least one address required. Not being able to remove the empty additional address means the content can't be edited.

NickGee’s picture

Hi there, I just want to share the work-around that I came up with, which I think will suffice our needs (pending user testing).

In our case, we needed at least one `proponent` address, and they were most likely going to be in Canada.

Step 1: I removed any required fields from the GUI and I removed any Country requirements as well.

Step 2: I added the following to mymodule_form_alter():

if ($node->hasField('field_proponent')) {
  // we need at least one proponent so make the first value required
  $form['field_proponent']['widget'][0]['address']['#required'] = TRUE;
  if ($node->isNew()) {
    // for new content, let's assume the first proponent will be in Canada 
    // will most likely be the case 99% of the time and users have the ability to change this value if they need
    $form['field_proponent']['widget'][0]['address']['#default_value']['country_code'] = 'CA';
  }
}

The above solution allows us to have a the first field required and set to a Canadian address for a better UX. Adding additional `proponents` is not as smooth an experience and we can't require them but this allows us to add and remove field values and allows us to save the form.

bojanz’s picture

Title: Multivalue widget - required field can only be saved first time, problems removing a value » Add support for unlimited value Address fields
Category: Bug report » Feature request
Priority: Major » Normal

Retitling, reclassifying. This is not something we've ever supported (D7 or D8), but there's no reason not to.

Marked #3005487: Address field + unlimited values causes node/add form to have 5 address fieldsets as duplicate.

gbirch’s picture

+1 for adding support for this. Our use case: database of health care practices, many of which have multiple office locations. Client wants to be able to search by practice characteristics (which we do NOT want to duplicate), and also to see where their offices are.

enriquelacoma’s picture

This happens because, when the field is set as required, all country fields are set as required. This patch checks the cardinality of the field and sets as required only the first one, this way it, when using a cardinality above 1, all additional instances are not required.

enriquelacoma’s picture

I did some refactoring to the test, so it is clearer.

joevagyok’s picture

Status: Active » Needs work

I tested the patch functionally and it works fine. Good job.
However, it needs some improvement on readability and some minor things. I will give you more specific list of comments in PM.

enriquelacoma’s picture

I updated the patch to improve comments and make the code more readable.

enriquelacoma’s picture

Status: Needs work » Needs review
joevagyok’s picture

Thank you @enriquelacoma. The patch looks good, tested locally and it works.

sabina.h’s picture

Status: Needs review » Reviewed & tested by the community

I've tested the patch from #13 and it works.

ar_howard’s picture

Patch#13 worked for me.

DamienMcKenna’s picture

Good work on the test coverage!

jsacksick’s picture

Status: Reviewed & tested by the community » Fixed

Committed! Thanks @enriquelacoma!

Status: Fixed » Closed (fixed)

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