Magento 2 : Add custom field in customer registration form without customer attribute create

magento.stackexchange https://magento.stackexchange.com/questions/295983

  •  20-03-2021
  •  | 
  •  

Pregunta

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

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top