I want to add custom field in customer registration form in magento2.
I don't want to make it customer attribute and want to save in custom table.
Can anyone help me
I want to add custom field in customer registration form in magento2.
I don't want to make it customer attribute and want to save in custom table.
Can anyone help me
Create a module named Vendor_Module. Create all the files and structure like below:
Step 1:
app/code/Vendor/Module/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Step 2:
app/code/Vendor/Module/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="Vendor_Module" setup_version="0.0.1">
<sequence>
<module name="Magento_Customer"/>
</sequence>
</module>
</config>
Step 3:
app/code/Vendor/Module/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\Customer\Block\Form\Register" name="additional_info_customer_custom_fields" template="Vendor_Module::customer/customfields.phtml" before="-"/>
</referenceContainer>
</body>
</page>
Step 4:
app/code/Vendor/Module/view/frontend/templates/customer/customfields.phtml
<div class="field custom required">
<label for="custom" class="label"><span><?= $block->escapeHtml(__('Custom')) ?></span></label>
<div class="control">
<input name="custom" type="text" value="" class="input-text" autocomplete="false" title="Custom" data-validate="{required:true}" />
</div>
</div>
Step 5:
app/code/Vendor/Module/etc/frontend/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_register_success">
<observer name="custom_register" instance="Vendor\Module\Observer\Accountcreate" shared="false" />
</event>
</config>
Step 6:
app/code/Vendor/Module/Observer/Accountcreate.php
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
class Accountcreate implements ObserverInterface
{
protected $request;
public function __construct(
... Inject Your custom model where your want to save the data
\Magento\Framework\App\Request\Http $request,
...
) {
...
$this->request = $request;
...
}
public function execute(Observer $observer)
{
$custom = $this->request->getParam('custom');
Now load your model and save the data, you will get the custom data in $custom field.
}
}
Run the necessary commands and test.
value=""
. That causes the field to be cleared if the registration form is reloaded because of an error (e.g. wrong CAPTCHA). How to set the current entered value so that user does not need to type the field value again? This feature works for default registration fields only.