When you have a required Address field hidden using Webform Conditionals settings, the field still is validated as required even if it is not displayed.

Example to reproduce (see screenshot):

  • Add a Select Options field (radios) with the following options: 0|No, 1|Yes
  • Add an Address field and make it required
  • Add a new Conditionals rule that says if Field 1 is Yes, then Show Address
  • Complete the first field by answering "No", which should keep the Address field hidden
  • Submit the form and notice that it does not pass validation requirements for Address fields

Like other webform field types, fields that are hidden using Conditionals should not be validated as required.

Webform 4.6

Comments

jeff.hartman’s picture

Issue summary: View changes
fonant’s picture

I have this problem too.

In fact even if the address field itself is not marked as being required, some of the sub-fields of the address are still marked as required.

fonant’s picture

Update: I was hitting the feature of addressfield that forces required fields if a country has been specified. More info: https://www.drupal.org/node/2479995

I think this means that if an addressfield is hidden by Webform Conditionals we might also need to set the country to "- None -" so that the sub-fields are not required.

Setting the default country for the possibly-hidden address to "- None -" solves the problem for me for most use-cases.

jeff.hartman’s picture

Setting the country to none does not work in my instance.

Additionally, it's not only when the fields are hidden using Webform Conditionals. The address will also be required on multi-page webforms when left empty and clicking "previous page."

saintnexcis’s picture

Has there been any progress on this? I'm running into the same problem.

alienzed’s picture

subscribing! same problem!

joel_osc’s picture

FYI, there is a quick workaround - just create your own module and use the alter hook this module provides:

function mymodule_field_widget_addressfield_standard_form_alter(&$element, $form_state, $context) {
  $element['street_block']['thoroughfare']['#required'] = FALSE;
  $element['locality_block']['locality']['#required'] = FALSE;
  $element['locality_block']['postal_code']['#required'] = FALSE;
  $element['locality_block']['administrative_area']['#required'] = FALSE;
}

A potential long-term solution would likely be to add a "Make all fields optional" option in the addressfield component config - the same way the addressfield field does and then turn off required on all of the fields.

chris burge’s picture

In a perfect world, we would rework the Addressfield module to handle required sub-fields the same same way the Name module does. I've written a patch that resolves this problem. I need to clean it up before posting, but it narrowly focuses on the original issue. It should be possible to expand it to include the use case described in in the last paragraph of comment #7.

chris burge’s picture

Status: Active » Needs review
StatusFileSize
new7.12 KB

Patch is attached. In order to fix this behavior without rewriting the Addressfield module, I ended up unsetting the #required attribute and passing it through as a 'data-' attribute. The patch adds a validation function, which handles validation instead of Addressfield.

Mike@TheWhippinpost’s picture

EDIT: Confirm #9 works for me - thanks Chris - although I'm seeing:

Notice: Undefined index: curr_address in _webform_addressfield_validate() (line 244 of [...]\addressfield_tokens\addressfield_tokens.components.inc).

Additionally, I'm seeing:

Warning: Invalid argument supplied for foreach() in _webform_addressfield_validate() (line 244 of [...]\addressfield_tokens\addressfield_tokens.components.inc).

Apologies for the confusion pre-edit.

ponies’s picture

Using this patch, https://www.drupal.org/project/addressfield_tokens/issues/2920845#commen..., allows the default country field to load an initial '- None -' value as opposed to the default country of the site. Which resolved my hidden shipping address validation issue.

amelio’s picture

Version: 7.x-1.5 » 7.x-1.11
StatusFileSize
new6.12 KB
new10.61 KB

Setting all address fields to optional wouldn't work for our use case, and I've applied the patch from #2920845 but setting the default country to None for a required address results in 'An illegal choice has been detected.' when submitting the form.

The patch in #9 doesn't work if the address is inside a fieldset. The attached patch fixes this by recursively checking through the submitted values (this is probably not the most efficient way of doing this, but it works).

It also fixes the warnings mentioned in #10, and cleans up the code a bit.

chris burge’s picture

Re #12, I can confirm that #9 doesn't work inside a fieldset. #12 corrects this issue. #12 tests successfully for us.

rhip’s picture

I needed to tweak patch 12 slightly after updating the addressfield_tokens module from 7.x-1.11 to 7.x-1.13 as the addressfield_tokens.components.inc file has changed slightly.

I think further changes will probably be needed again after the next release.

rhip’s picture

I noticed a mistake in my previous patch, this one corrects it. Made from branch 7.x-1.x

kerby024’s picture

RTBC +1 - last patch worked perfectly... Thanks!