Extend optional/required settings to address field for custom forms

Created on 24 May 2018, over 6 years ago
Updated 12 February 2023, almost 2 years ago

Awesome how the optional/required options are added to the field settings in the CMS.
This doesn't seem to extend when building a custom form with the address field though.

I currently make it required like this:

Add: $this->requiredFields[] = 'organization'; // Added this line to make organization required.
Below: $this->requiredFields = $definition['required_fields'];

Should be line 144 of webroot/vendor/commerceguys/addressing/src/AddressFormat/AddressFormat.php
Couldn't fine a better way to make the field required!

💬 Support request
Status

Closed: works as designed

Version

1.0

Component

Code

Created by

🇧🇪Belgium Pascal-

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.

  • 🇮🇳India mdmanouwer

    I have this configuration and work well
    please used following class
    use CommerceGuys\Addressing\AddressFormat\AddressField;
    use CommerceGuys\Addressing\AddressFormat\FieldOverride;

    $form['member_address_container']['address'] = [
    '#type' => 'fieldset',
    '#title' => 'Address',
    ];
    $form['member_address_container']['address']['who_is_paying_address'] = [
    '#type' =>'address',
    '#default_value' => ['country_code' => 'CA'],
    '#field_overrides' => [
    AddressField::ADDRESS_LINE1 => FieldOverride::OPTIONAL,
    AddressField::ADDRESS_LINE2 => FieldOverride::OPTIONAL,
    AddressField::POSTAL_CODE => FieldOverride::OPTIONAL,
    AddressField::GIVEN_NAME => FieldOverride::HIDDEN,
    AddressField::FAMILY_NAME => FieldOverride::HIDDEN,
    AddressField::ORGANIZATION => FieldOverride::HIDDEN,
    AddressField::ADMINISTRATIVE_AREA => FieldOverride::OPTIONAL,
    AddressField::LOCALITY => FieldOverride::OPTIONAL,

    ],

    '#available_countries' => ['CA'],
    ];

Production build 0.71.5 2024