1

I want to create a custom input field in Magento 2 user registration page and save this field in database. Can you help me ?

2

For add custom field in database create InstallSchema.php file

=> app/code/CompanyName/ModuleName/Setup/InstallSchema.php :

<?php
namespace CompanyName\ModuleName\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
{

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context )
    {
        $installer = $setup;

        $installer->startSetup();

        $eavTable = $installer->getTable('customer_entity');
        $columns = [
            'mobile' => [
                'type' => Table::TYPE_TEXT,
                'length' => 20,
                'nullable' => false,
                'default' => null,
                'comment' => 'mobile',
            ],
            //which field you want to create
        ];

        $connection = $installer->getConnection();
        foreach ($columns as $name => $definition) {
            $connection->addColumn($eavTable, $name, $definition);
        }

        $installer->endSetup();

    }
}

Create Layout file :

=> app/code/CompanyName/ModuleName/view/frontend/layout/customer_account_create.xml :

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

        <referenceBlock name="customer_form_register">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">CompanyName_ModuleName::form/register.phtml</argument>
            </action>
         </referenceBlock>
</page>

Now, get all code from /vendor/magento/module-customer/view/frontend/templates/form/register.phtml file and create your view.phtml file in your extension /view/frontend/templates/form/register.phtml

Then, add field in phtml file where you want to add.

For, Record save you can use observer :

First, add events.xml for execute observer file :

=> app/code/etc/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_save_after">
        <observer name="customer_register_success" instance="CompanyName\ModuleName\Observer\Savecustomer" />
    </event>

 </config>

Now, create observer file and add your code logic which you want to save :

=> app/code/Observer/Savecustomer.php :

<?php
namespace CompanyName\ModuleName\Observer;
use \Magento\Framework\App\ResourceConnection;
class Savecustomer implements \Magento\Framework\Event\ObserverInterface
{
    protected $_request;

    public function __construct(
    \Magento\Framework\App\RequestInterface $request,ResourceConnection $resource
    )
    {
        $this->_request = $request;
        $this->_resource = $resource;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
           ....................
           // Your code logic
           .................... 
    }
}

Hope this will helpful to you.

Happy Coding..!! :)

  • /vendor/magento/module-customer/view/frontend/templates/form/view.phtml i did'nt find this file – Mohd Hasan Apr 23 '18 at 9:33
  • I updated path in my answer. You can check it. – Emipro Technologies Pvt. Ltd. Apr 23 '18 at 9:37
  • ok, i try to find this – Mohd Hasan Apr 23 '18 at 9:42
  • Sure :) Let me know if any issue will generate. – Emipro Technologies Pvt. Ltd. Apr 23 '18 at 9:46
  • 1
    I am working on this. once i complete, i will accept this answer – Mohd Hasan Apr 23 '18 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.