Magento 2 – How to Add New Fields on Registration Page

custom-fieldmagento2registration

How to add a new field for the registration page in Magento 2?

Magento 2 company field added

Best Answer

If you want add new field, in customer account, you need to override the register.phtml in your custom theme.

Create custom theme, then create register.phtml in following path

app/design/frontend/vendor/theme/Magento_Customer/templates/form/register.phtml

Then, copy codes, from module-customer/view/frontend/templates/form/register.phtml and paste to above created file.

Then, add your custom field:

<div class="field required">
    <label for="custom_field" class="label"><span><?= __('CustomField') ?></span></label>
    <div class="control">
        <input type="text" name="custom_field" id="custom_field" value="<?= $block->escapeHtml($block->getFormData()->getCustomField()) ?>" title="<?= __('CustomField') ?>" class="input-text" data-validate="{required:true, 'validate-phoneStrict':true}">
    </div>
</div>

Before you do that, you need to create customer attribute for your custom_field to store the database.

Create the Customer Attribute:

You need to create a custom module to do that, after create Custom Module

Create InstallData.php in following path Vendor\Module\Setup

InstallData.php

In this below code I have added custom_field attribute.

  <?php
  /**
   * Copyright © 2016 Magento. All rights reserved.
   * See COPYING.txt for license details.
   */
  namespace Vendor\Module\Setup;

  use Magento\Customer\Setup\CustomerSetupFactory;
  use Magento\Customer\Model\Customer;
  use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
  use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
  use Magento\Framework\Setup\InstallDataInterface;
  use Magento\Framework\Setup\ModuleContextInterface;
  use Magento\Framework\Setup\ModuleDataSetupInterface;

  /**
   * Install data
   * @codeCoverageIgnore
   */
  class InstallData implements InstallDataInterface
  {

      /**
       * CustomerSetupFactory
       * @var CustomerSetupFactory
       */
      protected $customerSetupFactory;

      /**
       * $attributeSetFactory
       * @var AttributeSetFactory
       */
      private $attributeSetFactory;

      /**
       * initiate object
       * @param CustomerSetupFactory $customerSetupFactory
       * @param AttributeSetFactory $attributeSetFactory
       */
      public function __construct(
          CustomerSetupFactory $customerSetupFactory,
          AttributeSetFactory $attributeSetFactory
      )
      {
          $this->customerSetupFactory = $customerSetupFactory;
          $this->attributeSetFactory = $attributeSetFactory;
      }

      /**
       * install data method
       * @param ModuleDataSetupInterface $setup
       * @param ModuleContextInterface $context
       */
      public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
      {

          /** @var CustomerSetup $customerSetup */
          $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

          $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
          $attributeSetId = $customerEntity->getDefaultAttributeSetId();

          /** @var $attributeSet AttributeSet */
          $attributeSet = $this->attributeSetFactory->create();
          $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
          /**
           * customer registration form default field mobile number
           */
          $customerSetup->addAttribute(Customer::ENTITY, 'custom_field', [
              'type' => 'varchar',
              'label' => 'Custom Field',
              'input' => 'text',
              'required' => true,
              'visible' => true,
              'user_defined' => true,
              'sort_order' => 1000,
              'position' => 1000,
              'system' => 0,
          ]);
          //add attribute to attribute set
          $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile_number')
              ->addData([
                  'attribute_set_id' => $attributeSetId,
                  'attribute_group_id' => $attributeGroupId,
                  'used_in_forms' => ['adminhtml_customer', 'customer_account_create'],
              ]);

          $attribute->save();


      }
  }

After that run below command:

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
php bin/magento cache:clean

You will see your custom filed in registration form.

Let me know if you have issue.