Magento 2 Add Custom Field in Registration Form

0
42

Adding Customer EAV Attribute

Add custom fields or attribute on Magento 2 Registration form. This attribute also will show in add or edit customer form in Magento 2 Admin.

Creating Module

Create Rsgitech_Customer module.

module.xml

Create file module.xml app/code/Rsgitech/Customer/etc/module.xml

  <?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="Rsgitech_Customer" setup_version="1.0.0">          <sequence>              <module name="Magento_Customer"/>          </sequence>      </module>  </config>
Copy

registration.php

Create file registration.php app/code/Rsgitech/Customer/registration.phpfor register the module.

  <?php  \Magento\Framework\Component\ComponentRegistrar::register(      \Magento\Framework\Component\ComponentRegistrar::MODULE,      'Rsgitech_Customer',      __DIR__  );  ?>
Copy

InstallData.php

Customer attributes are created inside of InstallData and UpgradeData scripts. To add new attributes to the database, you must use the \Magento\Eav\Setup\EavSetupFactory class as a dependency injection.

Create file InstallData.php app/code/Rsgitech/Customer/Setup/InstallData.php for creating customer attribute.

  <?php  namespace Rsgitech\Customer\Setup;    use Magento\Customer\Model\Customer;  use Magento\Framework\Setup\ModuleContextInterface;  use Magento\Framework\Setup\ModuleDataSetupInterface;    class InstallData implements \Magento\Framework\Setup\InstallDataInterface  {      private $eavSetupFactory;            private $eavConfig;            private $attributeResource;            public function __construct(          \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,          \Magento\Eav\Model\Config $eavConfig,          \Magento\Customer\Model\ResourceModel\Attribute $attributeResource      ) {          $this->eavSetupFactory = $eavSetupFactory;          $this->eavConfig = $eavConfig;          $this->attributeResource = $attributeResource;      }            public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)      {          $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);            $eavSetup->removeAttribute(Customer::ENTITY, "skype");            $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);          $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);            $eavSetup->addAttribute(Customer::ENTITY, 'skype', [              // Attribute parameters              'type' => 'varchar',              'label' => 'Skype Account',              'input' => 'text',              'required' => true,              'visible' => true,              'user_defined' => true,              'sort_order' => 990,              'position' => 990,              'system' => 0,          ]);                    $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'skype');          $attribute->setData('attribute_set_id', $attributeSetId);          $attribute->setData('attribute_group_id', $attributeGroupId);            /*          //You can use this attribute in the following forms          adminhtml_checkout          adminhtml_customer          adminhtml_customer_address          customer_account_create          customer_account_edit          customer_address_edit          customer_register_address          */            $attribute->setData('used_in_forms', [              'adminhtml_customer',              'customer_account_create',              'customer_account_edit'          ]);            $this->attributeResource->save($attribute);      }  }  ?>
Copy

additional.phtml

Create view file additional.phtml app/code/Rsgitech/Customer/view/frontend/templates/additional.phtmlfor show field on customer registration form.

  <div class="field skype required">      <label class="label" for="skype">        <span><?= $block->escapeHtml(__('Skype Account')) ?></span>      </label>      <div class="control">         <input type="text" name="skype" id="skype" value="" title="<?= $block->escapeHtmlAttr(__('Skype Account')) ?>" class="input-text" data-validate="{required:true}">      </div>  </div>
Copy

customer_account_create.xml

Create layout file customer_account_create.xml app/code/Rsgitech/Customer/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="form_additional_info_customer" template="Rsgitech_Customer::additional.phtml"/>          </referenceContainer>      </body>  </page>
Copy

Run this command

  php bin/magento setup:upgrade  php bin/magento setup:static-content:deploy -f  php bin/magento cache:flush
Copy

Completed!!

 

Admin

How to Add Custom Field in Magento 2 Registration Form

Front

How to Add Custom Field in Magento 2 Registration Form

You can watch video on