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.
However, the custom attribute does not show up in the registration form.
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
Create Vendor/TestModule/view/frontend/templates/additionalinfocustomer.phtml
Clear cache and review frontend customer account registration page.