0

There are an option is required selected Please go to stores=> configuration => customer configuration and enable mobile number.

So let me know if is there anywhere else I need to change? Thanks in Advance.

0

You have to create Customer Attribute using a Custom module Please create a module as follows.

Create registration.php in app/code/Namespace/Module and add following code.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Namespace_Module',
    __DIR__
);

Create module.xml in app/code/Namespace/Module/etc and add following code.

<?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="Namespace_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

Create InstallData.php in app/code/Namespace/Module/Setup and add following code.

<?php


namespace Namespace\Module\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;

class InstallData implements InstallDataInterface
{

    private $customerSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'telephone', [
            'type' => 'varchar',
            'label' => 'Telephone',
            'input' => 'text',
            'source' => '',
            'required' => true,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'telephone')
        ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]
        ]);
        $attribute->save();
    }
}

Then add followinf code for Telephone input field to app/design/frontend/Vendor/Theme/Magento_Customer/templates/form/register.phtml

<div class="field required">
    <label for="customer_telephone" class="label"><span><?= $block->escapeHtml(__('Telephone')) ?></span></label>
    <div class="control">
        <input type="text" name="customer_telephone" id="customer_telephone" value="<?= $block->escapeHtmlAttr($block->getFormData()->getTelephone()) ?>" title="<?= $block->escapeHtmlAttr(__('Telephone')) ?>" data-mage-init='{"mage/trim-input":{}}' class="input-text" data-validate="{required:true, 'validate-number':true}">
    </div>
</div>
  • You with a custom field is ok. but there are already in the file so I just need to display. So let me know that solutions. – sarvesh Dineshkumar Patel Jul 8 at 12:12
  • The code you see in the registration.phtml file is for address fields, To show that fields on the registration form you have to create a file app/design/frontend/Vendor/Theme/Magento_Customer/layout/customer_account_create.xml And add the following code in body tag <referenceBlock name="customer_form_register"> <action method="setShowAddressFields"> <argument name="show.address.fields" xsi:type="boolean">true</argument> </action> </referenceBlock> – Kishor Thummar Jul 8 at 12:26
  • Yes, you are right but then extra fields also comes. I need to display only telephone field. thanks anyway. – sarvesh Dineshkumar Patel Jul 8 at 12:35
0

If you want add custom field, in customer account, Now you need to override the register.phtml in your custom theme.

Create custom theme, then create register.phtml in following path

app/design/frontend/vendor/theme/Magento_Customer/templates/form/register.phtml

Now, copy codes, from module-customer/view/frontend/templates/form/register.phtml and paste to above created file.

Then, add your custom field:

<div class="field required">
    <label for="custom_field" class="label"><span><?= __('CustomField') ?></span></label>
    <div class="control">
        <input type="text" name="custom_field" id="custom_field" value="<?= $block->escapeHtml($block->getFormData()->getCustomField()) ?>" title="<?= __('CustomField') ?>" class="input-text" data-validate="{required:true, 'validate-phoneStrict':true}">
    </div>
</div>

Before doing that, you have to create customer attribute for your custom_field to store the database.

Create the Customer Attribute:

You need to create a custom module to do that, after create Custom Module

Create InstallData.php in following path Vendor\Module\Setup

InstallData.php

In this below code I have added custon_field attribute.

<?php

  namespace Vendor\Module\Setup;

  use Magento\Customer\Setup\CustomerSetupFactory;
  use Magento\Customer\Model\Customer;
  use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
  use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
  use Magento\Framework\Setup\InstallDataInterface;
  use Magento\Framework\Setup\ModuleContextInterface;
  use Magento\Framework\Setup\ModuleDataSetupInterface;

  class InstallData implements InstallDataInterface
  {

      /**
       * CustomerSetupFactory
       * @var CustomerSetupFactory
       */
      protected $customerSetupFactory;

      /**
       * $attributeSetFactory
       * @var AttributeSetFactory
       */
      private $attributeSetFactory;

      /**
       * initiate object
       * @param CustomerSetupFactory $customerSetupFactory
       * @param AttributeSetFactory $attributeSetFactory
       */
      public function __construct(
          CustomerSetupFactory $customerSetupFactory,
          AttributeSetFactory $attributeSetFactory
      )
      {
          $this->customerSetupFactory = $customerSetupFactory;
          $this->attributeSetFactory = $attributeSetFactory;
      }

      /**
       * install data method
       * @param ModuleDataSetupInterface $setup
       * @param ModuleContextInterface $context
       */
      public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
      {

          /** @var CustomerSetup $customerSetup */
          $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

          $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
          $attributeSetId = $customerEntity->getDefaultAttributeSetId();

          /** @var $attributeSet AttributeSet */
          $attributeSet = $this->attributeSetFactory->create();
          $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
          /**
           * customer registration form default field mobile number
           */
          $customerSetup->addAttribute(Customer::ENTITY, 'custom_field', [
              'type' => 'varchar',
              'label' => 'Custom Field',
              'input' => 'text',
              'required' => true,
              'visible' => true,
              'user_defined' => true,
              'sort_order' => 1000,
              'position' => 1000,
              'system' => 0,
          ]);
          //add attribute to attribute set
          $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile_number')
              ->addData([
                  'attribute_set_id' => $attributeSetId,
                  'attribute_group_id' => $attributeGroupId,
                  'used_in_forms' => ['adminhtml_customer', 'customer_account_create'],
              ]);

          $attribute->save();


      }
  }

Now run below command:

php bin/magento setup:upgrade
php bin/magento cache:clean

You will see your custom filed in registration form.

Hope this will help you! Thanks.

  • You with a custom field is ok. but there are already in the file so I just need to display. So let me know that solutions. – sarvesh Dineshkumar Patel Jul 8 at 12:12

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.