I have created new address attribute hide_address with yes no value. Using setup script:

    if(version_compare($context->getVersion(), '1.0.2', '<')) {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSetup = $objectManager->create('Vendor\Module\Setup\CustomerSetup2');

In CustomerSetup2.php I have used this code:

public function installCustomerAddressAttributes($customerSetup)
            'label' => 'Hide Address',
            'system' => 0,
            'user_defined' => true,
            'position' => 100,
            'sort_order' => 100,
            'visible' => true,
            'default_value' => '',
            'note' => '',
            'type' => 'int',
            'input' => 'boolean',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',


    $customerSetup->getEavConfig()->getAttribute('customer_address', 'hide_address')->setData('is_user_defined', 1)->setData('default_value', '')->setData('used_in_forms', ['adminhtml_customer_address', 'customer_register_address', 'customer_address_edit'])->save();

Now I need to save it in a controller programmatically. I have used this code to save address:

$address = $this->addressDataFactory->create();
    ->setStreet(array('Line 1','Line 2'))

It is giving me this error:

Call to undefined method Magento\Customer\Model\Data\Address::setHideAddress()

Then I have tried this:

$address->setCustomAttribute('hide_address', 1); 

Using above code it is giving me this error:

Exception #0 (Magento\Framework\Validator\Exception): "Hide Address" is a required value.

I have also tried this:


Using above code it is giving me this error:

Exception #0 (Magento\Framework\Validator\Exception): "Hide Address" is a required value.

Please help me find the solution. I am unable to find any.
This is also not working for me:
Save or Update custom customer address attribute data

1 Answer 1


As you have using Api interface, So, you have to add hide_address as an extension attribute of Magento\Customer\Api\Data\AddressInterface.

Create a <Module>/etc/extension_attributes.xml file to define a hide_address extension attribute.

<?xml version="1.0"?>
 * @author Amit Bera
 * @copyright Copyright (c) 2018-2020 amitbera.com
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
        <attribute code="hide_address" type="int"/>

Save using below code:

    ->setStreet(array('Line 1','Line 2'))

$addressExtension = $address->getExtensionAttributes();
if(null === $addressExtension){
  // $addressExtensionFactory is instance of \Magento\Customer\Api\Data\AddressExtensionFactory
 $addressExtension =$addressExtensionFactory->create(); 


I am not sure why this is saving. Please assign attribute set to this attribute and see what happen:

/** @var \Magento\Eav\Setup\EavSetup $eavSetup */

Source: Magento2 : user define customer attribute not save value while create / save from admin

  • Many thanks Amit Bera. I am trying this one. Jun 25, 2019 at 5:14
  • You were right, I missed extension_attributes.xml, but now I am getting this error :( Uncaught Error: Cannot instantiate interface Magento\Customer\Api\Data\AddressExtensionInterface Jun 25, 2019 at 5:33
  • $addressExtensionFactory is instance of \Magento\Customer\Api\Data\AddressExtensionFactory
    – Amit Bera
    Jun 25, 2019 at 5:39
  • Sorry for this Amit. Not I am getting this error Exception #0 (Magento\Framework\Validator\Exception): "Hide Address" is a required value. I think we are close. I don't know why there is less documentation about extension_attributes Jun 25, 2019 at 6:04
  • please share your installer script
    – Amit Bera
    Jun 25, 2019 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.