2

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

2
  • Why you don't want to create an attribute, I'm curious to know reason behind it.
    – anonymous
    Commented Nov 18, 2019 at 11:47
  • 1
    Becuase I want to check on another page another to taking all customers customer field value that already exists or not!!! when we using flat table what i mean say is not eav table it quite faster than eav staructure. And client just too curious about speed Commented Nov 18, 2019 at 12:03

1 Answer 1

4

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.

2
  • 1
    Thanks @Sukumar for the very detailed answer. will check and approve once functionality working as expected. Commented Nov 18, 2019 at 11:43
  • Hello... in step 4 you used 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.
    – jstuardo
    Commented Jul 23, 2021 at 16:42

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.