I want to add a drop down field (Account type) and get this value in admin just like other fields values.
How can I add custom field in registration form
Magento – Magento2: How to add custom Drop down field in registration form
custom-fieldmagento2registration
Related Solutions
You need to create a module and here is installData.php
namespace Custom\Module\Setup;
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;
/**
* Init
*
* @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
*/
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* Installs DB schema for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "custom_field");
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "custom_field", array(
"type" => "varchar",
"backend" => "",
"label" => "Custom Field",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$custom_field = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "custom_field");
$custom_field = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_field');
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$custom_field->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 1002);
$custom_field->save();
$installer->endSetup();
}
}
Note : If you apply this script in existing module than, First you need to remove your module from Setup table from DB than run below commands to make it work.
setup:upgrade
setup:static-content:deploy
cache:clean
Adding a custom field in customer registration page
first if need then create a custom module follow this link
then need to create a file COMPANY\CUSTOMMODULE\Setup\InstallData.php
to creating custom field
<?php
namespace COMPANY\CUSTOMMODULE\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;
/**
* InstallData constructor.
* @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Customer\Model\ResourceModel\Attribute $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;
}
/**
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @throws \Magento\Framework\Exception\AlreadyExistsException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$customField = "custom_field";
$customFieldLabel = "Custom Field 1";
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->removeAttribute(Customer::ENTITY, $customField);
$attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
$attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);
$eavSetup->addAttribute(Customer::ENTITY, $customField, [
// Attribute parameters
'type' => 'varchar',
'label' => $customFieldLabel,
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'sort_order' => 990,
'position' => 990,
'system' => 0,
]);
$attribute = $this->eavConfig->getAttribute(Customer::ENTITY, $customField);
$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 // for admin page under customer edit account info
adminhtml_customer_address
customer_account_create // for store-front registration page
customer_account_edit // for store-front after logged-in account edit page
customer_address_edit
customer_register_address
*/
$attribute->setData('used_in_forms', [
'adminhtml_customer',
'customer_account_create',
'customer_account_edit'
]);
$this->attributeResource->save($attribute);
}
}
wait it will help to just create on DB level and visible in admin-level,
Need to access same custom field in store-front
so create a layout file COMPANY\CUSTOMMODULE\view\frontend\layout\customer_account_create.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="COMPANY_CUSTOMMODULE::additional.phtml"/>
</referenceContainer>
</body>
</page>
and to display field in store-front in registration page,
create a phtml file COMPANY\CUSTOMMODULE\view\frontend\templates\additional.phtml
<?php
$customFieldLabel = __("Custom Field 1");
$customField = "custom_field";
?>
<div class="field <?= $customField ?> required">
<label class="label" for="<?= $customField ?>">
<span><?= $block->escapeHtml($customFieldLabel) ?></span>
</label>
<div class="control">
<input type="text" name="<?= $customField ?>" id="<?= $customField ?>" value=""
title="<?= $block->escapeHtmlAttr($customFieldLabel) ?>"
class="input-text" data-validate="{required:true}">
</div>
</div>
after added these files or code
run below commands :
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
(for developer mode add -f to command)
php bin/magento cache:clear
For JS validation : In above phtml file (COMPANY\CUSTOMMODULE\view\frontend\templates\additional.phtml
), if you need to add custom validation then use Magento Validation classes to data-validate attribute.
More help for JS validation follow this link
Note : COMPANY\CUSTOMMODULE
is the custom module, if you need you can change with your custom module.
Hope this will help you.
Best Answer
Steps to add field in registration form:
Step 1: Create field (customer attribute) using
InstallData.php
Step 2: override
form/register.phtml
using layoutcustomer_account_create.xml
Step 3:
register.phtml
Step 4: Override edit.phtml file using
customer_account_edit.xml
Step 5:
form/edit.phtml
Step 6:
helper/data.php
Done!