Same as element validation needs to run earlier.

Created on 15 April 2025, 18 days ago

Problem/Motivation

My client has configured an address field 2 from the address module the first being an address, and the second being the postal. We have then address a same as element which will copy the address to the postal when the same element is checked it will copy the address to the postal field.

The problem is that there is element validation on the address fields which get called before the same as field. And since the postal field has been hidden there are no values and it doesn't pass validation since the address fields element validation is run before the copy as element validation.

Steps to reproduce

We have a custom version of the address element, where we have an autocomplete for the address, but keeps the country field is hidden, it has no data from the copy as the require validation gets tripped.

Proposed resolution

Right now the copy is being done from the "Copy as..." element validation which can be run after the source and destination element validation.

I am proposing that on the source and destination we insert a "Copy as..." validation which will get called as the first element validation of either the source or destination elements will do the copy first.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

6.2

Component

Code

Created by

🇦🇺Australia gordon Melbourne

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

Comments & Activities

  • Issue created by @gordon
  • Please share your code implementation so we can reproduce this bug.

  • 🇦🇺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.

Production build 0.71.5 2024