- Issue created by @gordon
- 🇦🇺Australia gordon Melbourne
@cilefen thanks for you prompt response.
I have been doing some more investigation, as when I looked at our address element is quite complex and depends on our custom setup. So I started breaking this down more so I could maybe create an element which would replicate the issue. Basically what the address element does is have an address lookup with a button which will allow you to switch between the single address field and a manual address field.
Thi issue actually looks like it is related to #3054786 → . Basically our address element will change the country to a hidden field with the country code set to the default value (in our case AU), and when we check the same as checkbox the value is set to nothing.
This would not be a big problem except the address element has
#element_validation
which gets run before the validation of the "Same as..." element (i.e. doing the copy) so there is no content set when the validation is checked. - 🇦🇺Australia gordon Melbourne
@cilefen I have managed to work out exactly what the problem is, and how to replicate it.
The problem is that if you set the country code as required, in the
WebformAddress::processAddress()
which we have done so that when you have a single valid country it will use a hidden value instead of a select.Here is some example code to recreate the issue.
namespace Drupal\example\Element; use Drupal\address\Element\Address; use Drupal\Core\Form\FormStateInterface; /** * Class ExampleAddress. * * @package Drupal\example\Element * * @FormElement("example_address_test") */ class ExampleAddressTest extends Address { public static function processAddress(array &$element, FormStateInterface $form_state, array &$complete_form) { $element = parent::processAddress($element, $form_state, $complete_form); $element['country_code']['#required'] = TRUE; return $element; } }
namespace Drupal\example\Plugin\WebformElement; use Drupal\webform\Plugin\WebformElement\Address; /** * Provides a 'address' element. * * @WebformElement( * id = "example_address_test", * label = @Translation("Advanced address test"), * description = @Translation("Provides advanced element for storing, validating and displaying international postal addresses."), * category = @Translation("Composite elements"), * composite = TRUE, * multiline = TRUE, * states_wrapper = TRUE, * dependencies = { * "address", * } * ) * * @see \Drupal\address\Element\Address */ class ExampleAddressTest extends Address { }
If you use these WebformElement and Element, you can set up a simple webform which has a Same as element to copy from the address to the postal address on submission. Be sure to set up the addresses with a single country associated.
please note that there is an issue when running this that the element will not show any of the field but if you just hit submit it will replicate the issue.
Basically as soon as you click on the Same as checkbox the country code for the postal address will get the single country value removed. thus when you submit the form, the require value validation of the country code will get run before the Same as validation which copies the values from one to the other, and then you get this stray error on the country code hidden field.
As a work around I have made a change so that country code element is set to disabled so that the form api will disreguard lack of a value from the post and use the default value which was initial set, and the stray error will not get called.