Magento – Magento 2 – Add custom attribute in customer registration form

attributeseavmagento2register

I want to add a custom attribute in customer registration form. I write a module with the following InstallData.php

<?php

namespace vendor\TestModule\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface {

    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    public function __construct(CustomerSetupFactory $customerSetupFactory) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $setup->startSetup();
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(Customer::ENTITY, 'client_idn', [
            'label' => 'Client IDN',
            'input' => 'text',
            'required' => false,
            'sort_order' => 40,
            'visible' => true,
            'system' => false,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => true]
        );

        // add attribute to form
        /** @var  $attribute */
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'client_idn');
        $attribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create']);
        $attribute->save();

        $setup->endSetup();
    }

}

In database, the records are successfully inserted.
enter image description here
enter image description here

However, the custom attribute does not show up in the registration form.
enter image description here

Is there anything wrong?

Best Answer

If your install data script successfully installed your custom attribute, now you just need to override addition information phtml file and set your custom attribute in that.

Create Vendor/TestModule/view/frontend/layout/customer_account_create.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="form.additional.info">
        <block class="Magento\Framework\View\Element\Template" name="additional_info_customer_client_idn" template="Vendor_TestModule::additionalinfocustomer.phtml"/>
    </referenceContainer>
</body>
</page>

Create Vendor/TestModule/view/frontend/templates/additionalinfocustomer.phtml

<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
    <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend>
    <p>
    <div class="field regulation">
        <label for="regulation" class="label"><span><?php /* @escapeNotVerified */
                echo __('Client IDN') ?></span></label>
        <div class="control">
            <input type="text" name="client_idn" id="client_idn" title="<?php /* @escapeNotVerified */ echo __('Client IDN') ?>" class="input-text" data-validate="{required:false}">
        </div>
    </div>
    </p>
</fieldset>

Clear cache and review frontend customer account registration page.

Related Topic