I want to add custom fields to my Magento 2 customer registration form. Should I use a DB UpgradeData script to add fields to the Magento DB table, or should I add my own table?

If I add my own table, how do I get my custom data and the default data to show up in the same place?



1 Answer 1


You have to create a module to add the extra field to form and save with customer data, Try the below way. In this module, I have added the GST Number field.




<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
            <module name="Magento_Customer"/>



namespace Vendor\Module\Setup;

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

class InstallData implements InstallDataInterface

    private $customerSetupFactory;

     * Constructor
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;

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

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'gst_number', [
            'type' => 'varchar',
            'label' => 'GST Numner',
            'input' => 'text',
            'source' => '',
            'required' => false,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'gst_number')
        ->addData(['used_in_forms' => [


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


<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */echo __('* Required Fields') ?>"> 
    <legend class="legend">
        <span><?php /* @escapeNotVerified */echo __('Additional Information') ?></span>
    <div class="field gst_number required"> 
        <label class="label" for="email">
            <span><?= $block->escapeHtml(__('GST Number')) ?></span>
        <div class="control"> 
            <input type="text" name="gst_number" id="gst_number" title="<?php /* @escapeNotVerified */echo __('My Attribute') ?>" class="input-text" data-validate="{required:true}" autocomplete="off"> 

Hope this will work for you.

  • Thank you for this. This does answer my question, and tells me that I need to create an entire new module just to add a field, but when it comes to customer editing data, what do I have to do then? Create another module for editing?
    – web master
    Commented Mar 5, 2020 at 13:40
  • 1
    It all runs, but it doesn't add the field to the form
    – web master
    Commented Mar 6, 2020 at 17:00

Your Answer

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

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