I have created a custom attribute for customer in Magento 2 site.

The HTML of the field is this:

        <input type="text"
               name="<?= $block->escapeHtmlAttr($block->getFieldName('custom')) ?>"
               value="<?= $block->escapeHtmlAttr($block->getObject()->getCustom()) ?>"
               title="<?= $block->escapeHtmlAttr($block->getAttributeData()->getFrontendLabel('custom')) ?>"
               data-validate="{required:true, 'validateData':true}">

Look at all $block calls. None of them works.

$block->getFieldName('custom') returns an empty string

$block->getObject()->getCustom() throws an exception because getObject is null

$block->getAttributeData()->getFrontendLabel('custom') throws an exception because $block->getAttributeData() is null.

How can I do it?

The field value is something that should be retrieved dynamically, because if there is an error in the registration form, and the page is reloaded, all fields should be repopulated. This works for all standard fields but not for the custom ones.


This is how I am creating the attribute:

public function addCustomAttribute()
    $eavSetup = $this->eavSetupFactory->create();

            'type' => 'varchar',
            'label' => 'Custom label',
            'input' => 'text',
            'required' => 1,
            'visible' => 1,
            'user_defined' => 1,
            'sort_order' => 1,
            'position' => 1,
            'system' => 0,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => true,

    $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
    $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

    $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'custom');
    $attribute->setData('attribute_set_id', $attributeSetId);
    $attribute->setData('attribute_group_id', $attributeGroupId);

    $attribute->setData('used_in_forms', [

  • How you created this fields? Jul 23, 2021 at 13:35
  • @DhirenVasoya the standard way to do it, as shown in this page: store.magenest.com/blog/…. I think I need to create a model, but I have not found information about this. It seems nobody cares about re-populating the field when page is reloaded.
    – jstuardo
    Jul 23, 2021 at 13:41
  • @DhirenVasoya I have updated the question just with the code that created the attribute.
    – jstuardo
    Jul 23, 2021 at 14:27

2 Answers 2


You need to assign the files into this forms as well.

$attribute->setData('used_in_forms', [
  • This is how I have it now: $attribute->setData('used_in_forms', [ 'adminhtml_checkout', 'adminhtml_customer', 'customer_account_create', 'customer_account_edit', 'checkout_index_index' ])..... and to test, in the edit form, I had this: name="<?= $block->escapeHtmlAttr($block->getFieldName('custom')) ?>". The result, name attribute is empty.
    – jstuardo
    Jul 26, 2021 at 15:37

Since there is no a simple way to do this, I have just hardcoded the input field attributes. It is better than complicating things creating a custom block class just for this.

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.